Thymeleaf manual rendering
In order to improve the speed of page views, you can cache html page, the client requests retrieved from the cache, and then not get the manual rendering
In 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; } // get the Product List 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);
In spring5
@Autowired
ThymeleafViewResolver thymeleafViewResolver;
model.addAttribute ( " the User " , the User); // Query Product List 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);