欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
    }

 

相关标签: Themeleaf手动渲染