Thymeleaf手动渲染

为提高页面访问速度,可缓存html页面,客户端请求从缓存获取,获取不到再手动渲染

在spring4下

@Autowired
ThymeleafViewResolver thymeleafViewResolver; @Autowired
ApplicationContext applicationContext;
public String list(HttpServletRequest request, HttpServletResponse response, Model model) {

        //取缓存
String html = redisService.get("goods_list", String.class);
if(!StringUtils.isEmpty(html)) {
return html;
}
//获取商品列表
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
//手动渲染
SpringWebContext ctx = new SpringWebContext(request,response,
request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
//写缓存
if(!StringUtils.isEmpty(html)) {
redisService.set("goods_list", html);
}
return html;


 @Autowired
private ThymeleafViewResolver thymeleafViewResolver; SpringWebContext ctx = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(),
model.asMap(), applicationContext);
// 手动渲染
html = thymeleafViewResolver.getTemplateEngine().process("这里写html页面名称", ctx);

在spring5

@Autowired
ThymeleafViewResolver thymeleafViewResolver;
model.addAttribute("user", user);
//查询商品列表
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
if (!StringUtils.isEmpty(html)){
return html;
} WebContext ctx = new WebContext(request,response,request.getServletContext(),
request.getLocale(),model.asMap());
thymeleafViewResolver.getTemplateEngine().process("goods_list",ctx); return "goods_list";


 @Autowired
private ThymeleafViewResolver thymeleafViewResolver; WebContext ctx = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
// 手动渲染
html = thymeleafViewResolver.getTemplateEngine().process("这里写html页面名称", ctx);

最新文章

  1. 验证ASP.NET页生命周期时间的触发顺序
  2. NSLog 占位符
  3. js sql C#时间、时间戳相互转换
  4. SharePoint 2013 Silverlight中使用Net客户端对象模型
  5. (Extjs)对于GridPanel的各种操作
  6. 【unity shaders】:Unity中的Shader及其基本框架
  7. 配置svn
  8. Linux摄像头驱动学习之:(一)V4L2_框架分析
  9. 设置Linux下Mysql表名不区分大小写
  10. [JavaEE] 深入理解Struts2的ognl标签
  11. LwIP源代码文件目录解析
  12. Natural Language Processing with Python - Chapter 0
  13. [转载]再谈iframe自适应高度
  14. [Locked] Maximum Size Subarray Sum Equals k
  15. 转载 LayoutInflater的inflate函数用法详解
  16. mc面试题记录
  17. C# 泛型反射的调用
  18. u盘辨别
  19. overlay fs挂载及操作测试
  20. OCSP

热门文章

  1. Openstack组件实现原理 — Nova 体系结构
  2. (转载)前端构建工具gulpjs的使用介绍及技巧
  3. redis集群创建时报错:Sorry, can&#39;t connect to node
  4. 高效IO之Dex加密(三)
  5. C# 设计模式 (一)
  6. CentOS6.5下RPM包安装MySQL5.6.35
  7. canvas 画一条折线
  8. 我要多开梦幻手游PC端(梦幻手游PC端多开的简单分析及实现办法)(二)
  9. 前端自动化构建工具——gulp环境搭建教程
  10. Linux系统的buff/cache缓存清理脚本