美文网首页
springboot集成thymeleaf,一个接口既可以返回j

springboot集成thymeleaf,一个接口既可以返回j

作者: 爱的旋转体 | 来源:发表于2019-12-17 17:53 被阅读0次

    1.注入

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;
    

    2.接口

        @GetMapping("/a")
        @ResponseBody
        public String handle(@RequestParam Boolean flag ,HttpServletRequest request,
                HttpServletResponse response, Model model) {
            if(flag) {
                //手动渲染
                IWebContext  ctx = new WebContext(request,response,
                        request.getServletContext(),request.getLocale(), model.asMap());
                return thymeleafViewResolver.getTemplateEngine().process("页面", ctx);
            }else {
                return "json";
            }
        }
    

    参考:1.Thymeleaf手动渲染,可做页面缓存;
    2.解决Sping5中SpringWebContext方法过时;

    相关文章

      网友评论

          本文标题:springboot集成thymeleaf,一个接口既可以返回j

          本文链接:https://www.haomeiwen.com/subject/xhhenctx.html