Thymeleaf手动渲染,可做页面缓存.
程序员文章站
2022-06-04 10:59:17
...
注入两个类
@Autowired
ThymeleafViewResolver thymeleafViewResolver;
@Autowired
ApplicationContext applicationContext;
渲染方法
@RequestMapping(value="/to_list", produces="text/html")
@ResponseBody
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;
}