乐优商城笔记六:商品详情页
使用模板引擎 Thymeleaf + nginx 完成商品详情页静态化

完成乐优商城商品详情页

搭建商品详情页微服务

创建子工程

  • GroupId:com.leyou.service
  • ArtifactId:ly-page

编写pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>com.leyou</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.leyou.service</groupId>
<artifactId>ly-page</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.leyou.service</groupId>
<artifactId>ly-item-interface</artifactId>
<version>${leyou.latest.version}</version>
</dependency>
</dependencies>
</project>

编写application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8004
spring:
application:
name: page-service
thymeleaf:
cache: false # 关闭thymeleaf缓存
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9999/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期

编写启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.leyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class LyPageService {
public static void main(String[] args) {
SpringApplication.run(LyPageService.class, args);
}
}

导入thymeleaf模板

将下面的代码放入resources/templates下。

item.html

这个模板为最终模板,即内部逻辑已经全部完成,只需要放入数据即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>乐优商城--商品详情页</title>
<link rel="icon" href="/assets/img/favicon.ico">

<link rel="stylesheet" type="text/css" href="/css/webbase.css" />
<link rel="stylesheet" type="text/css" href="/css/pages-item.css" />
<link rel="stylesheet" type="text/css" href="/css/pages-zoom.css" />
<link rel="stylesheet" type="text/css" href="/css/widget-cartPanelView.css" />

<style type="text/css">
.goods-intro-list li {
display: inline-block;
width: 300px;
}
.Ptable {
margin: 10px 0;
}
.Ptable-item {
padding: 12px 0;
line-height: 220%;
color: #999;
font-size: 12px;
border-bottom: 1px solid #eee;
}
.Ptable-item h3 {
width: 110px;
text-align: right;
}
.Ptable-item h3, .package-list h3 {
font-weight: 400;
font-size: 12px;
float: left;
}
h3 {
display: block;
font-size: 1.17em;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
font-weight: bold;
}
.Ptable-item dl {
margin-left: 110px;
}
dl {
display: block;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
}
.Ptable-item dt {
width: 160px;
float: left;
text-align: right;
padding-right: 5px;
}
.Ptable-item dd {
margin-left: 210px;
}
dd {
display: block;
-webkit-margin-start: 40px;
}
.package-list {
padding: 12px 0;
line-height: 220%;
color: #999;
font-size: 12px;
margin-top: -1px;
}
.package-list h3 {
width: 130px;
text-align: right;
}
.package-list p {
margin-left: 155px;
padding-right: 50px;
}
</style>

</head>

<body>

<!-- 头部栏位 -->
<!--页面顶部,由js动态加载-->
<div id="itemApp">
<div id="nav-bottom">
<ly-top />
</div>
<div class="py-container">
<div id="item">
<div class="crumb-wrap">
<ul class="sui-breadcrumb">
<li th:each="category : ${categories}">
<a href="#" th:text="${category.name}">手机</a>
</li>
<li>
<a href="#" th:text="${brand.name}">Apple</a>
</li>
<li class="active" th:text="${spu.title}">Apple iPhone 6s</li>
</ul>
</div>
<!--product-info-->
<div class="product-info">
<div class="fl preview-wrap">
<!--放大镜效果-->
<div class="zoom">
<!--默认第一个预览-->
<div id="preview" class="spec-preview">
<span class="jqzoom">
<img :jqimg="images[0]" :src="images[0]" width="400px" height="400px"/>
</span>
</div>
<!--下方的缩略图-->
<div class="spec-scroll">
<a class="prev">&lt;</a>
<!--左右按钮-->
<div class="items">
<ul>
<li v-for="(image, i) in images" :key="i">
<img :src="image" :bimg="image" onmousemove="preview(this)" />
</li>
</ul>
</div>
<a class="next">&gt;</a>
</div>
</div>
</div>
<div class="fr itemInfo-wrap">
<div class="sku-name">
<h4 v-text="sku.title"></h4>
</div>
<div class="news"><span th:utext="${spu.subTitle}"></span></div>
<div class="summary">
<div class="summary-wrap">
<div class="fl title"><i>价  格</i></div>
<div class="fl price">
<i>¥</i><em v-text="ly.formatPrice(sku.price)"></em><span>降价通知</span>
</div>
<div class="fr remark"><i>累计评价</i><em>612188</em></div>
</div>
<div class="summary-wrap">
<div class="fl title">
<i>促  销</i>
</div>
<div class="fl fix-width">
<i class="red-bg">加价购</i>
<em class="t-gray">满999.00另加20.00元,或满1999.00另加30.00元,或满2999.00另加40.00元,即可在购物车换
购热销商品</em>
</div>
</div>
</div>
<div class="support">
<div class="summary-wrap">
<div class="fl title">
<i>支  持</i>
</div>
<div class="fl fix-width">
<em class="t-gray">以旧换新,闲置手机回收 4G套餐超值抢 礼品购</em>
</div>
</div>
<div class="summary-wrap">
<div class="fl title">
<i>配 送 至</i>
</div>
<div class="fl fix-width">
<em class="t-gray">上海 <span>有货</span></em>
</div>
</div>
</div>
<div class="clearfix choose">
<div id="specification" class="summary-wrap clearfix">
<dl v-for="(value,key) in specialSpec" :key="key">
<dt>
<div class="fl title">
<i>{{key}}</i>
</div>
</dt>
<dd v-for="(str, index) in value" :key="index">
<a href="javascript:;" :class="{selected: index===indexes[key]}" @click="indexes[key]=index">
{{str}}<span v-if="index===indexes[key]" title="点击取消选择">&nbsp;</span>
</a>
</dd>
</dl>
</div>

<div class="summary-wrap">
<div class="fl title">
<div class="control-group">
<div class="controls">
<input autocomplete="off" type="text" disabled value="1" minnum="1" class="itxt" />
<a href="javascript:void(0)" class="increment plus">+</a>
<a href="javascript:void(0)" class="increment mins">-</a>
</div>
</div>
</div>
<div class="fl">
<ul class="btn-choose unstyled">
<li>
<a href="success-cart.html" target="_blank" class="sui-btn btn-danger addshopcar">加入购物车</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!--product-detail-->
<div class="clearfix product-detail">
<div class="fl aside">
<ul class="sui-nav nav-tabs tab-wraped">
<li class="active">
<a href="#index" data-toggle="tab">
<span>相关分类</span>
</a>
</li>
<li>
<a href="#profile" data-toggle="tab">
<span>推荐品牌</span>
</a>
</li>
</ul>
<div class="tab-content tab-wraped">
<div id="index" class="tab-pane active">
<ul class="part-list unstyled">
<li>手机</li>
<li>手机壳</li>
<li>内存卡</li>
<li>Iphone配件</li>
<li>贴膜</li>
<li>手机耳机</li>
<li>移动电源</li>
<li>平板电脑</li>
</ul>
<ul class="goods-list unstyled">
<li>
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/part01.png" />
</div>
<div class="attr">
<em>Apple苹果iPhone 6s (A1699)</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>6088.00</i>
</strong>
</div>
<div class="operate">
<a href="javascript:void(0);" class="sui-btn btn-bordered">加入购物车</a>
</div>
</div>
</li>
<li>
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/part02.png" />
</div>
<div class="attr">
<em>Apple苹果iPhone 6s (A1699)</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>6088.00</i>
</strong>
</div>
<div class="operate">
<a href="javascript:void(0);" class="sui-btn btn-bordered">加入购物车</a>
</div>
</div>
</li>
<li>
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/part03.png" />
</div>
<div class="attr">
<em>Apple苹果iPhone 6s (A1699)</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>6088.00</i>
</strong>
</div>
<div class="operate">
<a href="javascript:void(0);" class="sui-btn btn-bordered">加入购物车</a>
</div>
</div>
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/part02.png" />
</div>
<div class="attr">
<em>Apple苹果iPhone 6s (A1699)</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>6088.00</i>
</strong>
</div>
<div class="operate">
<a href="javascript:void(0);" class="sui-btn btn-bordered">加入购物车</a>
</div>
</div>
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/part03.png" />
</div>
<div class="attr">
<em>Apple苹果iPhone 6s (A1699)</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>6088.00</i>
</strong>
</div>
<div class="operate">
<a href="javascript:void(0);" class="sui-btn btn-bordered">加入购物车</a>
</div>
</div>
</li>
</ul>
</div>
<div id="profile" class="tab-pane">
<p>推荐品牌</p>
</div>
</div>
</div>
<div class="fr detail">
<div class="clearfix fitting">
<h4 class="kt">选择搭配</h4>
<div class="good-suits">
<div class="fl master">
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/l-m01.png" />
</div>
<em>¥5299</em>
<i>+</i>
</div>
</div>
<div class="fl suits">
<ul class="suit-list">
<li class="">
<div id="">
<img src="/img/_/dp01.png" />
</div>
<i>Feless费勒斯VR</i>
<label data-toggle="checkbox" class="checkbox-pretty">
<input type="checkbox"><span>39</span>
</label>
</li>
<li class="">
<div id=""><img src="/img/_/dp02.png" /> </div>
<i>Feless费勒斯VR</i>
<label data-toggle="checkbox" class="checkbox-pretty">
<input type="checkbox"><span>50</span>
</label>
</li>
<li class="">
<div id=""><img src="/img/_/dp03.png" /></div>
<i>Feless费勒斯VR</i>
<label data-toggle="checkbox" class="checkbox-pretty">
<input type="checkbox"><span>59</span>
</label>
</li>
<li class="">
<div id=""><img src="/img/_/dp04.png" /></div>
<i>Feless费勒斯VR</i>
<label data-toggle="checkbox" class="checkbox-pretty">
<input type="checkbox"><span>99</span>
</label>
</li>
</ul>
</div>
<div class="fr result">
<div class="num">已选购0件商品</div>
<div class="price-tit"><strong>套餐价</strong></div>
<div class="price">¥5299</div>
<button class="sui-btn btn-danger addshopcar">加入购物车</button>
</div>
</div>
</div>
<div class="tab-main intro">
<ul class="sui-nav nav-tabs tab-wraped">
<li class="active">
<a href="#one" data-toggle="tab">
<span>商品介绍</span>
</a>
</li>
<li>
<a href="#two" data-toggle="tab">
<span>规格与包装</span>
</a>
</li>
<li>
<a href="#three" data-toggle="tab">
<span>售后保障</span>
</a>
</li>
<li>
<a href="#four" data-toggle="tab">
<span>商品评价</span>
</a>
</li>
<li>
<a href="#five" data-toggle="tab">
<span>手机社区</span>
</a>
</li>
</ul>
<div class="clearfix"></div>
<div class="tab-content tab-wraped">
<div id="one" class="tab-pane active">
<!--商品详情-->
<div class="intro-detail">
<div th:utext="${detail.description}"></div>
</div>
</div>
<div id="two" class="tab-pane">
<div class="Ptable">
<div class="Ptable-item" v-for="(group, i) in specs" :key="i">
<h3 v-text="group.group"></h3>
<dl>
<span v-for="(param,j) in group.params" :key="j">
<dt v-text="param.k"></dt>
<dd v-text="param.v ? param.v + (param.unit || '') : JSON.parse(sku.ownSpec)[param.k]"></dd>
</span>
</dl>
</div>
</div>
<div class="package-list">
<h3>包装清单</h3>
<p th:text="${detail.packingList}"></p>
</div>

</div>
<div id="three" class="tab-pane">
<p>售后保障</p>
<p th:text="${detail.afterService}"></p>
</div>
<div id="four" class="tab-pane">
<p>商品评价</p>
</div>
<div id="five" class="tab-pane">
<p>手机社区</p>
</div>
</div>
</div>
</div>
</div>
<!--like-->
<div class="clearfix"></div>
<div class="like">
<h4 class="kt">猜你喜欢</h4>
<div class="like-list">
<ul class="yui3-g">
<li class="yui3-u-1-6">
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/itemlike01.png" />
</div>
<div class="attr">
<em>DELL戴尔Ins 15MR-7528SS 15英寸 银色 笔记本</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>3699.00</i>
</strong>
</div>
<div class="commit">
<i class="command">已有6人评价</i>
</div>
</div>
</li>
<li class="yui3-u-1-6">
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/itemlike02.png" />
</div>
<div class="attr">
<em>Apple苹果iPhone 6s/6s Plus 16G 64G 128G</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>4388.00</i>
</strong>
</div>
<div class="commit">
<i class="command">已有700人评价</i>
</div>
</div>
</li>
<li class="yui3-u-1-6">
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/itemlike03.png" />
</div>
<div class="attr">
<em>DELL戴尔Ins 15MR-7528SS 15英寸 银色 笔记本</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>4088.00</i>
</strong>
</div>
<div class="commit">
<i class="command">已有700人评价</i>
</div>
</div>
</li>
<li class="yui3-u-1-6">
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/itemlike04.png" />
</div>
<div class="attr">
<em>DELL戴尔Ins 15MR-7528SS 15英寸 银色 笔记本</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>4088.00</i>
</strong>
</div>
<div class="commit">
<i class="command">已有700人评价</i>
</div>
</div>
</li>
<li class="yui3-u-1-6">
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/itemlike05.png" />
</div>
<div class="attr">
<em>DELL戴尔Ins 15MR-7528SS 15英寸 银色 笔记本</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>4088.00</i>
</strong>
</div>
<div class="commit">
<i class="command">已有700人评价</i>
</div>
</div>
</li>
<li class="yui3-u-1-6">
<div class="list-wrap">
<div class="p-img">
<img src="/img/_/itemlike06.png" />
</div>
<div class="attr">
<em>DELL戴尔Ins 15MR-7528SS 15英寸 银色 笔记本</em>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>4088.00</i>
</strong>
</div>
<div class="commit">
<i class="command">已有700人评价</i>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>

</div>
<script src="/js/vue/vue.js"></script>
<script src="/js/axios.min.js"></script>
<script src="/js/common.js"></script>

<script th:inline="javascript">
// sku集合
const skus = /*[[${skus}]]*/ [];
// 规格参数id与name对
const paramMap = /*[[${specs}]]*/ {};
// 特有规格参数集合
const specialSpec = JSON.parse(/*[[${detail.specTemplate}]]*/ "");
// 初始化特有规格参数默认选中一个
const indexes = {};
const initIndex = skus[0].indexes.split("_");

Object.keys(specialSpec).forEach((key, i) => {
indexes[key] = parseInt(initIndex[i]);
});
const indexArr = skus.map(s => s.indexes);

const specs = JSON.parse(/*[[${detail.specifications}]]*/ "");

</script>

<script>
var itemVm = new Vue({
el:"#itemApp",
data:{
ly,
skus,
paramMap,
specialSpec,
indexes,
specs
},
components:{
lyTop: () => import('/js/pages/top.js')
},
computed: {
sku(){
const index = Object.values(this.indexes).join("_");
return this.skus.find(s=>s.indexes==index);
},
images(){
return this.sku.images ? this.sku.images.split(",") : [''];
}
},
});
</script>

<script type="text/javascript" src="/js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#service").hover(function(){
$(".service").show();
},function(){
$(".service").hide();
});
$("#shopcar").hover(function(){
$("#shopcarlist").show();
},function(){
$("#shopcarlist").hide();
});

})
</script>
<script type="text/javascript" src="/js/model/cartModel.js"></script>
<script type="text/javascript" src="/js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="/js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="/js/plugins/jquery.jqzoom/jquery.jqzoom.js"></script>
<script type="text/javascript" src="/js/plugins/jquery.jqzoom/zoom.js"></script>
<script type="text/javascript" src="index/index.js"></script>
</body>

</html>

使用thymeleaf渲染页面

创建FeignClient

  • 分别创建 BrandClient,CategoryClient,GoodsClient和SpecificationClient接口并继承item-interface中对应的API接口类即可。

  • 提供api,基本上所有的api已经在之前就准备好了,只需要在GoodsController中新增一个api。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
      
    /**
    * 查询spu信息
    *
    * @param spuId 商品ID
    * @return Spu
    */
    @GetMapping("/{spuId}")
    public ResponseEntity<SpuBO> queryGoodsById(@PathVariable("spuId") Long spuId) {
    return ResponseEntity.ok(goodsService.queryGoodsById(spuId));
    }

    goodsService.queryGoodsById(spuId)在之前已经实现,只需要将api对外提供即可。

PageServiceConstants

这是我的常量类,PageService用到的一些字符串常量,我都定义在这里面的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.leyou.page.util;

/**
* page service 常量类
*/
public final class PageServiceConstants {

/**
* template name
*/
public static final String TEMPLATE_NAME_ITEM = "item";

/**
* Model field
*/
public static final String MODEL_CATEGORIES = "categories";
public static final String MODEL_BRAND = "brand";
public static final String MODEL_SPU = "spu";
public static final String MODEL_DETAIL = "detail";
public static final String MODEL_SKUS = "skus";
public static final String MODEL_SPECS = "specs";

}

Controller代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.leyou.page.controller;

import com.leyou.page.service.PageService;
import com.leyou.page.util.PageServiceConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

@Controller
public class PageController {

@Autowired
private PageService pageService;

/**
* 生成数据并返回到item视图
*/
@GetMapping("item/{id}.html")
public String spuPageInfo(@PathVariable("id") Long spuId, Model model) {
// 获取数据
Map<String, Object> attributes = pageService.loadModel(spuId);
// 封装数据
model.addAllAttributes(attributes);

return PageServiceConstants.TEMPLATE_NAME_ITEM;
}
}

Service代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.leyou.page.service;

import com.alibaba.fastjson.JSON;
import com.leyou.BO.SpuBO;
import com.leyou.client.item.BrandClient;
import com.leyou.client.item.CategoryClient;
import com.leyou.client.item.GoodsClient;
import com.leyou.client.item.SpecificationClient;
import com.leyou.page.util.PageServiceConstants;
import com.leyou.pojo.Brand;
import com.leyou.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {

@Autowired
private GoodsClient goodsClient;

@Autowired
private CategoryClient categoryClient;

@Autowired
private BrandClient brandClient;

@Autowired
private SpecificationClient specificationClient;

public Map<String, Object> loadModel(Long spuId) {
Map<String, Object> model = new HashMap<>();
// 获取spu信息
SpuBO spuBO = goodsClient.queryGoodsById(spuId);
// 分类数据
List<Category> categories = categoryClient.queryCategoryListByIds(Arrays.asList(spuBO.getCid1(), spuBO.getCid2(), spuBO.getCid3()));
// 品牌数据
Brand brand = brandClient.queryBrandById(spuBO.getBrandId());

model.put(PageServiceConstants.MODEL_BRAND, brand);
model.put(PageServiceConstants.MODEL_CATEGORIES, categories);
model.put(PageServiceConstants.MODEL_DETAIL, spuBO.getSpuDetail());
model.put(PageServiceConstants.MODEL_SKUS, spuBO.getSkuList());
model.put(PageServiceConstants.MODEL_SPECS, JSON.parseObject(spuBO.getSpuDetail().getSpecTemplate()));
model.put(PageServiceConstants.MODEL_SPU, spuBO);

return model;
}

}

运行前的工作

修改搜索页的跳转

修改nginx配置

之前所有的www.leyou.com域名下的地址都会转发到9002端口,现在我们需要将/item的请求转发到8004端口,也就是我们的page-service的端口

启动nginx,即可测试啦。

网页静态化

什么是静态化

静态化是指把动态生成的HTML页面变为静态内容保存,以后用户的请求到来,直接访问静态页面,不再经过服务的渲染。

而静态的HTML页面可以部署在nginx中,从而大大提高并发能力,减小服务器访问压力。

如何实现静态化

目前,静态化页面都是通过模板引擎来生成,而后保存到nginx服务器来部署。常用的模板引擎比如:

  • Freemarker
  • Velocity
  • Thymeleaf

我们之前就使用的Thymeleaf,来渲染html返回给用户。Thymeleaf除了可以把渲染结果写入Response,也可以写到本地文件,从而实现静态化。

thymeleaf重要角色

Context:运行上下文

用来保存模型数据,当模板引擎渲染时,可以从Context上下文中获取数据用于渲染。

当与SpringBoot结合使用时,我们放入Model的数据就会被处理到Context,作为模板渲染的数据使用。

TemplateResolver:模板解析器

用来读取模板相关的配置,例如:模板存放的位置信息,模板文件名称,模板文件的类型等等。

当与SpringBoot结合时,TemplateResolver已经由其创建完成,并且各种配置也都有默认值,比如模板存放位置,其默认值就是:templates。比如模板文件类型,其默认值就是html。

TemplateEngine:模板引擎

模板引擎:用来解析模板的引擎,需要使用到上下文、模板解析器。分别从两者中获取模板中需要的数据,模板文件。然后利用内置的语法规则解析,从而输出解析后的文件。

通过templateEngine.process方法执行解析和页面生成过程

代码实现

Service代码

PageService中新增代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Autowired
private TemplateEngine templateEngine;

@Value("ly.page.destPath")
private String pagePath;

/**
* 生成商品详情页并保存到指定目录
*
* @param spuId 商品ID
*/
public void createHtml(Long spuId) {
Context context = new Context();
context.setVariables(loadModel(spuId));

// 输出流
File file = new File(pagePath + "/" + spuId + ".html");
try(PrintWriter printWriter = new PrintWriter(file)) {
// 生成页面
templateEngine.process(PageServiceConstants.TEMPLATE_NAME_ITEM, context, printWriter);
} catch (FileNotFoundException e) {
log.error("[生成商品详情页失败,商品ID = {}]", spuId, e);
}
}
Controller代码

修改PageController中的spuPageInfo方法

配置nginx代理静态资源

修改之前代理到8004端口的配置信息

采用静态页和tomcat加载两种方式,静态页目录未找到该静态页,就从数据库加载数据。

性能优化

生成商品详情页,使用线程池去执行生成静态页,进一步提高效率。

新增工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.leyou.page.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadUtils {

private static final ExecutorService es = Executors.newFixedThreadPool(10);

public static void execute(Runnable runnable) {
es.submit(runnable);
}
}

PageService新增方法

1
2
3
4
5
6
7
8
/**
* 新建线程处理页面静态化
*
* @param spuId 商品ID
*/
public void asyncExcute(Long spuId) {
ThreadUtils.execute(() -> createHtml(spuId));
}

然后修改controller中的生成静态页方法的调用为 asyncExcute(spuId)即可。

遗留问题

做到这里,会想到,这个静态页生成了就一直不会变了,万一我的商品有了修改,或者下架了怎么办,怎么去完成商品信息的同步呢?使用服务间的调用?还是其他方式?

这个问题在下一个笔记中详细处理,会使用到消息中间件去完成商品信息的一个同步过程。

文章作者: imxushuai
文章链接: https://www.imxushuai.com/2002/01/01/乐优商城笔记六:商品详情页/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 imxushuai
支付宝打赏
微信打赏