@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; } //获取商品列表 ListgoodsList = 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; }复制代码