您的位置:首页 > 其它

18.03.25,web学习第八十四天,商城day10,freemarker、网页静态化方案、freemarker模板

2018-03-25 17:07 537 查看

84商城day10

freemarker

1. 昨天展示商品详情使用的是jsp+redis来减轻数据库压力
   现提供另一种减轻数据库压力的方案:

2. 实现网页静态化freemarker

  可使用nginx直接访问静态资源
1)它是模板引擎,主要做静态页面
   使用步骤:<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>使用步骤:@Test
public void genFile() throws Exception {
// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是freemarker对于的版本号。
Configuration configuration = new Configuration(Configuration.getVersion());
// 第二步:设置模板文件所在的路径。
configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-itcast/term197/e3-item-web/src/main/webapp/WEB-INF/ftl"));
// 第三步:设置模板文件使用的字符集。一般就是utf-8.
configuration.setDefaultEncoding("utf-8");
// 第四步:加载一个模板,创建一个模板对象。
Template template = configuration.getTemplate("hello.ftl");
// 第五步:创建一个模板使用的数据集,可以是pojo也可以是map。一般是Map。
Map dataModel = new HashMap<>();
//向数据集中添加数据
dataModel.put("hello", "this is my first freemarker test.");
// 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。
Writer out = new FileWriter(new File("D:/temp/term197/out/hello.html"));
// 第七步:调用模板对象的process方法输出文件。
template.process(dataModel, out);
// 第八步:关闭流。
out.close();
}模板:遵循el表达式
${hello}

3. Freemark模板语法

1)访问map中的key:${key}
2)访问pojo中的属性:${key.property}
3)取集合中的数据



取循环的下标
<#list studentList as student>
${student_index}
</#list>
判断:
<#if student_index % 2 == 0>
<#else>
</#if>
日期类型:
 


Null值的处理
 


<#include “模板名称”>相当于jsp的include
 


4. Freemarker整合SpringMVC

1)导入jar包
 


2)创建整合springmvc的配置文件<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMark
c276
erConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>
黄色其实是对configuration 的封装
@Controller
public class HtmlGenController {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;

@RequestMapping("/genhtml")
@ResponseBody
public String genHtml()throws Exception {
// 1、从spring容器中获得FreeMarkerConfigurer对象。
// 2、从FreeMarkerConfigurer对象中获得Configuration对象。
Configuration configuration = freeMarkerConfigurer.getConfiguration();
// 3、使用Configuration对象获得Template对象。
Template template = configuration.getTemplate("hello.ftl");
// 4、创建数据集
Map dataModel = new HashMap<>();
dataModel.put("hello", "1000");
// 5、创建输出文件的Writer对象。
Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));
// 6、调用模板对象的process方法,生成文件。
template.process(dataModel, out);
// 7、关闭流。
out.close();
return "OK";
}
}

5. 网页静态化方案

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


6. Jsp改为freemarker模板

1)改造开始,这里用的是相对路径,相对于当前页面的路径
 


 




Fmt标签删除改为直接取。
 
2)写监听消息的逻辑package cn.e3mall.item.controller;

import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import cn.e3mall.pojo.TbItem;
import cn.e3mall.pojo.TbItemDesc;
import cn.e3mall.service.ItemService;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class ItemAddListener implements MessageListener{
@Autowired
private ItemService is;
@Autowired
private FreeMarkerConfigurer freemarkerConfig;
@Override
public void onMessage(Message message) {
try {
TextMessage tm=(TextMessage) message;
String text = tm.getText();
Long id = new Long(text);
Thread.sleep(1000);
TbItem itemById = is.getItemById(id);
Item item = new Item(itemById);
TbItemDesc tid = is.tid(id);
Map map=new HashMap<>();
map.put("item",item);
map.put("itemDesc",tid);
Configuration configuration = freemarkerConfig.getConfiguration();
Template template = configuration.getTemplate("item.ftl");
Writer writer=new FileWriter("C:/Users/Administrator.8N69VMO2VWQO06Q/Desktop/ftl"+id+".html");
template.process(map, writer);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}3)配置监听器applicationContext-activemq,
   还有配置FreeMarkerConfigurer
4) Web.xml必须配置
 


5)如果需要在windows版测试,可安装windows版的nginx
6)那如何访问这个商品详情信息的静态化资源呢?将搜索出来的商
   品的url改为图片服务器额度地址即可。
7)在转为ftl模板时,注意:可能为空的要改为判断

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

解决方案:
1、配置tomcat集群。配置tomcatSession复制。节点数不要超过5个。
2、可以使用Session服务器,保存Session信息,使每个节点是无状态。需要模拟Session。
用redis模拟session
单点登录系统是使用redis模拟Session,实现Session的统一管理。
1)参考content整合sso(pom),新建的interface和service都是module,web层为project继承自parent
2)如果springmvc的前端控制器配置为/,那么静态资源文件放在哪里都可以,需要配置静态资源映射。
如果配置为*.html,那么静态资源就得放在就得webapp下。配置*.html叫做伪静态化。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: