首页 > 编程语言 > 详细

springboot整合thymeleaf手动渲染

时间:2019-10-11 16:06:03      阅读:367      评论:0      收藏:0      [点我收藏+]

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

 

springboot整合thymeleaf手动渲染

原文:https://www.cnblogs.com/xiufengchen/p/11653726.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!