We were on the festival website Freemaker achieve static test, and now we realize in the project application.
See the previous section:
Freemarker achieve static pages (a)
Original link: https://blog.csdn.net/qq_36335126/article/details/103881330
- Product Details static pages
2.1 Static scheme pages
Name of the output file: Product id + ". Html"
Path of the output file: any directory outside the project.
Web access: using nginx web access. Under this scenario only a tomcat role is to generate static pages.
Project deployment: can e3-item-web deployed on multiple servers.
Generate static pages when: After adding commodities to generate static pages. You can use Activemq, subscribe topic (items to add)
Static architecture diagram:
2.2. The transformation of freemarker template jsp
Create a template: Copy all the contents of the folder to the next jsp ftl folder, and instead to .ftl .jsp suffix ending.Jsp corresponding original content inside the corresponding syntax instead of ftl。
The corresponding change ftl syntax similar to the following, not introduced.
Jsp remove formatted digital form, the values directly.
The forEach loop through the array instead of ftl syntax.
2.3. MessageListener achieve
pom file to add dependent jar package:
Documents directory structure:
the MessageListener class
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = null;
Long itemId = null;
// 取商品id
if (message instanceof TextMessage) {
textMessage = (TextMessage) message;
itemId = Long.parseLong(textMessage.getText());
// 等待事务提交
Thread.sleep(1000);
//取Item内容
TbItem tbItem = itemService.getItemById(itemId);
//对图片的处理用我们新创建的pojo
Item item = new Item(tbItem);
//取商品描述
TbItemDesc itemDesc = itemService.getItemDescById(itemId);
//创建一个Configuration对象。
Configuration configuration = freeMarkerConfigurer.getConfiguration();
//加载一个模板,创建一个模板对象。
Template template = configuration.getTemplate("item.ftl");
//创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
Map dataModel = new HashMap<>();
//向数据集中添加数据
dataModel.put("item", item);
dataModel.put("itemDesc", itemDesc);
//创建一个输出流,指定输出的目录及文件名。
Writer out = new FileWriter(HTML_GEN_PATH + itemId + ".html");
//调用模板对象的process方法输出文件。
template.process(dataModel, out);
//第八步:关闭流。
out.close();
}
} catch (Exception e) {
// TODO: handle exception
}
2.4. MessageListener monitor configuration items to add messages
resource.p roperties file configuration
applicationContext-activemq.xml
<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:util="http://www.springframework.org/schema/util" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.131: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="item-change-topic" />
</bean>
<!-- 接收消息 -->
<!-- 配置监听器 -->
<bean id="htmlGenListener" class="cn.e3mall.item.listener.HtmlGenListener" />
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="topicDestination" />
<property name="messageListener" ref="htmlGenListener" />
</bean>
</beans>
* Load the configuration file to modify servlet start Web.xml
2.5. Test static pages
Run the program, add merchandise management page, and then view the correspondence generated itemId .html files in the directory, indicating the test is successful.
2.6. The static page deployment to the project.
(We are conveniently test windows version of nginx)
Install nginx version of windows directly nginx-1.8.0.zip extract to the specified directory (Directory can not contain Chinese) Open to run.
Nginx start-up mode and linux version, there is not much talk about it.
Nginx.conf configuration (change directory)
to reload the configuration.
Static files via http ways:
The corresponding CSS, images, js folder to the E: \ temp \ freemarker \ (root directory) which can generate a complete static pages.
Display effect:
then the original URL generated dynamically changed by url nginx generated can be achieved static files.