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

2019-12-17  本文已影响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方法过时;

上一篇下一篇

猜你喜欢

热点阅读