Fork me on GitHub

十.宜立方商城——freemarker实现网页静态化以及ActiveMq同步生成静态网页

一.计划

1
2
1、使用freemarker实现网页静态化
2、ActiveMq同步生成静态网页

二.网页静态化

可以使用Freemarker实现网页静态化。

2.1 什么是freemarker

FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。

目前企业中:主要用Freemarker做静态页面或是页面展示

2.2 Freemarker的使用方法

把freemarker的jar包添加到工程中。

Maven工程添加依赖

1
2
3
4
5
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
原理:

image

1
2
3
4
5
6
7
8
9
使用步骤:
第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
第二步:设置模板文件所在的路径。
第三步:设置模板文件使用的字符集。一般就是utf-8.
第四步:加载一个模板,创建一个模板对象。
第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
第七步:调用模板对象的process方法输出文件。
第八步:关闭流。

模板:

${hello}

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
/**
* @author RickYinPeng
* @ClassName FreeMarkerTest
* @Description
* @date 2018/12/2/17:55
*/
public class FreeMarkerTest {

@Test
public void testFreeMarkerTest() throws Exception{
//1、创建一个模板文件
//2、创建一个Configuration对象
Configuration configuration = new Configuration(Configuration.getVersion());

//3、设置模板文件保存的目录
configuration.setDirectoryForTemplateLoading(new File("E:\\IdeaWorpace\\e_Store\\e3_item_web\\src\\main\\webapp\\WEB-INF\\ftl"));

//4、模板文件的编码格式,一般就是utf-8
configuration.setDefaultEncoding("utf-8");

//5、加载一个模板文件,创建一个模板对象
// Template template = configuration.getTemplate("hello.ftl");
Template template = configuration.getTemplate("student.ftl");

//6、创建一个数据集。可以是pojo也可以是map。推荐使用map
Map data = new HashMap<>();
data.put("hello","hello freemark");

//创建一个pojo对象
Student student = new Student(1,"HCY",19,"三宝聚源");
data.put("student",student);

//添加一个List
List<Student> stuList = new ArrayList<>();
stuList.add(new Student(1,"HCY1",18,"三宝聚源"));
stuList.add(new Student(2,"HCY2",19,"三宝聚源"));
stuList.add(new Student(3,"HCY3",20,"三宝聚源"));
stuList.add(new Student(4,"HCY4",21,"三宝聚源"));
stuList.add(new Student(5,"HCY5",22,"三宝聚源"));
stuList.add(new Student(6,"HCY6",23,"三宝聚源"));
stuList.add(new Student(7,"HCY7",24,"三宝聚源"));
data.put("stuList",stuList);

//7、创建一个Writer对象,指定输出文件的路径及文件名
// Writer out = new FileWriter(new File("G:\\freemark\\hello.txt"));
Writer out = new FileWriter(new File("G:\\freemark\\student.html"));

//8、生成静态页面
template.process(data,out);

//9、关闭流
out.close();
}
}

2.3 模板的语法

2.3.1 访问map中的key

${key}

2.3.2 访问pojo中的属性

Student对象。学号、姓名、年龄

${key.property}

image

2.3.3 取集合中的数据

1
2
3
4
5
<#list studentList as student>

${student.id}/${studnet.name}

</#list>

image

2.3.4 取循环中的下标

1
2
3
<#list studentList as student>
${student_index}
</#list>

image

image

2.3.5 判断

1
2
3
4
5
<#if student_index % 2 == 0>

<#else>

</#if>

image

2.3.6 日期类型格式化

image

2.3.7 Null值的处理

image

2.3.8 Include标签

1
<#include “模板名称”>

image

2.4 创建整合spring的配置文件

得需要先导入一个spring-context-support的jar包,这个我们之前导入过

2.4.1 创建整合spring的配置文件

在我们的springmvc.xml文件中加入如下代码(就是将我们之前用的freemark中的类配置到spring容器中)
1
2
3
4
5
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

image

image

需要编写一Controller进行测试

2.4.2 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
请求的url:/genhtml
参数:无
返回值:ok (String, 需要使用@ResponseBody)
业务逻辑:
1、从spring容器中获得FreeMarkerConfigurer对象。
2、从FreeMarkerConfigurer对象中获得Configuration对象。
3、使用Configuration对象获得Template对象。
4、创建数据集
5、创建输出文件的Writer对象。
6、调用模板对象的process方法,生成文件。
7、关闭流。

加载配置文件:
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
/**
* @author RickYinPeng
* @ClassName HtmlGenController
* @Description 生成静态页面测试
* @date 2018/12/6/20:59
*/
@Controller
public class HtmlGenController {

@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;

@RequestMapping("/genhtml")
@ResponseBody
public String genHtml() throws IOException, TemplateException {
Configuration configuration = freeMarkerConfigurer.getConfiguration();
//加载模板对象
Template template = configuration.getTemplate("hello.ftl");
//创建一个数据集
Map data = new HashMap<>();
data.put("hello","你好世界!!!");
//指定文件输出的路径及文件名
Writer writer = new FileWriter("G:\\freemark\\hello2.html");
//输出文件
template.process(data,writer);
//关闭流
writer.close();
return "OK";
}
}

image

image

image

2.5 商品详情页面静态化

2.5.1 网页的静态化方案

  1. 输出文件的名称:商品id+“.html”
  2. 输出文件的路径:工程外部的任意目录。
  3. 网页访问:使用nginx访问网页。在此方案下tomcat只有一个作用就是生成静态页面。
  4. 工程部署:可以把e3-item-web部署到多个服务器上。
  5. 生成静态页面的时机:商品添加后,生成静态页面。可以使用Activemq,订阅topic(商品添加)

image

2.5.2 实现步骤

  1. 先看看之前实现的消息接收和发送

image

image

  1. 导入相关activemq的jar包

image

  1. 实现监听消息接收的监听器

image

image

  1. 将jsp改造为模板

image

image

image

image

image

image

image

  1. 实现监听器
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
/**
* @author RickYinPeng
* @ClassName HtmlGenListener
* @Description
* @date 2018/12/7/13:41
*/
public class HtmlGenListener implements MessageListener {

@Autowired
private ItemService itemService;

@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;

@Value("${HTML_GEN_PATH}")
private String HTML_GEN_PATH;

@Override
public void onMessage(Message message) {
try {
//创建一个模板,参考jsp

//从消息中取出商品id
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
Long itemId = new Long(text);

//等待事务提交
Thread.sleep(1000);

//根据商品id查询商品信息,商品基本信息和商品描述
TbItem tbItem = itemService.getItemById(itemId);
Item item = new Item(tbItem);

//去商品描述信息
TbItemDesc itemDesc = itemService.getItemDescById(itemId);

//创建一个数据集,把商品数据封装
Map data = new HashMap<>();
data.put("item", item);
data.put("itemDesc", itemDesc);

//加载模板对象
Configuration configuration = freeMarkerConfigurer.getConfiguration();
Template template = configuration.getTemplate("item.ftl");

//创建一个输出流,指定输出流的目录及文件名
Writer out = new FileWriter(HTML_GEN_PATH + itemId + ".html");
//生成静态页面
template.process(data, out);
//关闭流
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

image

image

image

image

image

image

  1. 配置商品监听消息在spring容器中(我们之前在search_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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.128:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>

<!--这个是主题目的地,一对多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="itemAddTopic" />
</bean>
<!-- 接收消息 -->
<!-- 配置监听器 -->
<bean id="htmlGenListener" class="yp.e3mall.item.listener.HtmlGenListener"></bean>
<!-- 监听商品添加容器,同步索引库 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="topicDestination" />
<property name="messageListener" ref="htmlGenListener"/>
</bean>


</beans>

image

image

image

image

image

image

image

三.Sso系统分析

3.1 什么是sso系统


SSO英文全称Single Sign On,单点登录。SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。它包括可以将这次主要的登录映射到其他应用中用于同一个用户的登录的机制。它是目前比较流行的企业业务整合的解决方案之一。

3.2 为什么要有单点登录系统

3.2.1 传统的登录实现方式

image
image

此方式在只有一个web工程时是没有问题。

3.2.2 集群环境下

image
         image

集群环境下会出现要求用户多次登录的情况。


解决方案:
1. 配置tomcat集群。配置tomcatSession复制。节点数不要超过5个。
2. 可以使用Session服务器,保存Session信息,使每个节点是无状态。需要模拟Session。

单点登录系统是使用redis模拟Session,实现Session的统一管理。