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";
}
}