Thymeleaf手动渲染

Thymeleaf手动渲染

为提高页面访问速度,可缓存html页面,客户端请求从缓存获取,获取不到再手动渲染

在spring4下


import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

 @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

SpringWebContext ctx = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(),
                model.asMap(), applicationContext);
// 手动渲染
html = thymeleafViewResolver.getTemplateEngine().process("这里写html页面名称", ctx);

在spring5下Thymeleaf手动渲染略有不同


import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;


 @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;


WebContext ctx = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
// 手动渲染
html = thymeleafViewResolver.getTemplateEngine().process("这里写html页面名称", ctx);

你可能感兴趣的:(项目实战)