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