springboot整合thymeleaf⼿动渲染Thymeleaf⼿动渲染
为提⾼页⾯访问速度,可缓存html页⾯,客户端请求从缓存获取,获取不到再⼿动渲染
在spring4下
@Autowired
ThymeleafViewResolver thymeleafViewResolver;
@Autowired
ApplicationContext applicationContext;
public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
//取缓存
String html = ("goods_list", String.class);
if(!StringUtils.isEmpty(html)) {
return html;
}
//获取商品列表thyme
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
//⼿动渲染
SpringWebContext ctx = new SpringWebContext(request,response,
html = TemplateEngine().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, ServletContext(), Locale(), model.asMap(), applicationContext);
// ⼿动渲染
html = TemplateEngine().process("这⾥写html页⾯名称", ctx);
在spring5
@Autowired
ThymeleafViewResolver thymeleafViewResolver;
model.addAttribute("user", user);
//查询商品列表
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
String html = (GoodsList, "", String.class);
if (!StringUtils.isEmpty(html)){
return html;
}
WebContext ctx = new WebContext(request,ServletContext(),
return"goods_list";
@Autowired
private ThymeleafViewResolver thymeleafViewResolver;
WebContext ctx = new WebContext(request,ServletContext(),Locale(),model.asMap()); // ⼿动渲染
html = TemplateEngine().process("这⾥写html页⾯名称", ctx);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论