使用FreeMaker机制实现高并发优化
来源:互联网 发布:软件开发报价方案 编辑:程序博客网 时间:2024/06/05 08:06
在实际开发项目过程中,经常会使用Redis缓存机制减轻数据库的访问压力,其实还有一个方案可以实现数据库的访问优化。就是使用FreeMaker机制。当访问一次数据库后把数据封装到静态html页面上,以后再访问相同网站时就不需要再次查询数据,直接加载显示html页面。
下面做了一个实例优化,代码如下:
@Controllerpublic class FreeMakerController { @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @RequestMapping("/genhtml") public String genHtml(HttpServletRequest request, HttpServletResponse response)throws Exception { //1、从spring容器中获得FreeMarkerConfigurer对象。 //2、从FreeMarkerConfigurer对象中获得Configuration对象。 Configuration configuration = freeMarkerConfigurer.getConfiguration(); //3、使用Configuration对象获得Template对象。相应的ftl模板我已经放到项目工程中去了,此项目我放在/WEB-INF/ftl目录下 Template template = configuration.getTemplate("hello.ftl"); //4、创建数据集 Map dataModel = new HashMap<>(); //需要把数据放到Map集合中去 dataModel.put("hello", "1000"); //5、创建输出文件的Writer对象,创建HTML文件 File file = new File(request.getServletContext().getRealPath("/WEB-INF/html") + "/html","hello.html"); File dir = file.getParentFile(); if (!dir.exists()) { dir.mkdirs(); } try { if (!file.exists()) { file.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } Writer out = new FileWriter(file); //6、调用模板对象的process方法,数据封装到文件中去。 template.process(dataModel, out); //7、关闭流。 out.close(); //直接显示已加载好数据的HTML文件,而不是jsp文件 return "/html/hello.html"; }}
属性配置:
<mvc:resources location="/WEB-INF/html/html/" mapping="/html/*.html"/><mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/*.html"/><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>
在实践过程我也遇到一些问题:
1)一开始在Springmvc.xml中配置jsp过滤属性
<!-- 配置视图解析器 作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 前缀--> <property name="prefix" value="/WEB-INF/jsp/"></property> <!--后缀--> <property name="suffix" value=".jsp"></property></bean>
但是我又配置了
<mvc:resources location="/WEB-INF/html/html/" mapping="/html/*.html"/><mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/*.html"/>
无法正常访问网页,原来会优先进行前后缀的过滤,比如我返回的值是/html/hello.html,返回网址是”/WEB-INF/html/html/hello.html”,但是实际访问的网址是”/WEB-INF/jsp/html/hello.html.jsp”导致无法访问,需要把前后缀过滤注释掉。
阅读全文
0 0
- 使用FreeMaker机制实现高并发优化
- Nginx实现高并发优化
- Nginx实现高并发优化
- ConcurrentHashMap 高并发性的实现机制
- ConcurrentHashMap 高并发性的实现机制
- ConcurrentHashMap 高并发性的实现机制
- ConcurrentHashMap 高并发性的实现机制
- Mysql高并发优化
- Mysql高并发优化
- 高并发优化
- MySql高并发优化
- 高并发优化方法
- nginx高并发优化
- 高并发性能优化
- 高并发优化解决方案
- tomcat高并发优化
- 服务器高并发优化
- 高并发的优化
- 机器学习中的正则化
- C语言单链表面试题2
- 学习笔记---overload VS overrid
- Excel VBA高效办公应用-第七章-VBA财务报表分析-Part3 (现金流量表分析)
- thread27
- 使用FreeMaker机制实现高并发优化
- 震惊!做了这题就会了快速幂+扩展欧几里德+bsgs(附扩展bsgs)
- 关于Spring大容器和SpringMVC小容器中注解扫描冲突(影响事物回滚)的解决
- laravel
- 深入理解计算机系统(第三版) 练习2.13
- 外观模式(门面模式)--Facade
- 基于opencv的车牌识别(一)开章及任务详述
- 利用json-server创建伪接口
- illumination invariant image之恢复到彩色图像