zoukankan      html  css  js  c++  java
  • springboot整合thymeleaf手动渲染

    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);
  • 相关阅读:
    python爬取斗图网中的 “最新套图”和“最新表情”
    SpringBoot (1) idea下的环境搭建及demo
    python爬取视频网站m3u8视频,下载.ts后缀文件,合并成整视频
    微信小程序—day05
    从零起步做到Linux运维经理, 你必须管好的23个细节
    前后端分离原理
    图文并茂|为你揭开微服务架构的“神秘面纱”!
    swarm集群日常部分操作
    OpenStack 部署运维实战
    京东618:Docker扛大旗,弹性伸缩成重点 (2015-06-23)
  • 原文地址:https://www.cnblogs.com/xiufengchen/p/11653726.html
Copyright © 2011-2022 走看看