18.03.25,web学习第八十四天,商城day10,freemarker、网页静态化方案、freemarker模板
2018-03-25 17:07
537 查看
84商城day10
现提供另一种减轻数据库压力的方案:
1、配置tomcat集群。配置tomcatSession复制。节点数不要超过5个。
2、可以使用Session服务器,保存Session信息,使每个节点是无状态。需要模拟Session。
用redis模拟session
单点登录系统是使用redis模拟Session,实现Session的统一管理。
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叫做伪静态化。 |
相关文章推荐
- 学习淘淘商城第七十一课(网页静态化-freemarker模板改造)
- JAVA学习笔记28——网页静态化之Freemarker(二)
- 学习淘淘商城第七十课(网页静态化方案分析)
- Freemarker 网页静态化(二) - 模板
- 淘淘商城系列——网页静态化——把jsp改造为freemarker模板
- 利用freemarker 静态化网页
- VS2013中Python学习笔记[Django Web的第一个网页]
- 软件开发过程学习笔记(五)之测试方案模板
- 利用freemarker 静态化网页
- Spring Boot 与 Kotlin使用Freemarker模板引擎渲染web视图
- Spring Boot☞ 使用freemarker模板引擎渲染web视图
- 利用freemarker 静态化网页
- 如何使用freemarker将jsp网页静态化?
- 解决vs2010 utimate中文版添加Silverlight for WP7模板方案【WP7学习札记之一】
- 利用freemarker 静态化网页
- 如何使用freemarker将jsp网页静态化?
- WebView不同版本网页自适配问题解决方案
- 利用freemarker 静态化网页
- web开发-邮件编写HTML网页正常显示实现方法-学习笔记八
- 网页数据采集-webcollector学习与实践