springboot整合thymeleaf手动渲染
Thymeleaf手动渲染
为提高页面访问速度,可缓存html页面,客户端请求从缓存获取,获取不到再手动渲染
在spring4下
@Autowired
ThymeleafViewResolver thymeleafViewResolver; @Autowired
ApplicationContext applicationContext;
public String list(HttpServletRequest request, HttpServletResponse response, Model model) { //取缓存
String html = redisService.get("goods_list", String.class);
if(!StringUtils.isEmpty(html)) {
return html;
}
//获取商品列表
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
//手动渲染
SpringWebContext ctx = new SpringWebContext(request,response,
request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
html = thymeleafViewResolver.getTemplateEngine().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, request.getServletContext(), request.getLocale(),
model.asMap(), applicationContext);
// 手动渲染
html = thymeleafViewResolver.getTemplateEngine().process("这里写html页面名称", ctx);
在spring5
@Autowired
ThymeleafViewResolver thymeleafViewResolver;
model.addAttribute("user", user);
//查询商品列表
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
if (!StringUtils.isEmpty(html)){
return html;
} WebContext ctx = new WebContext(request,response,request.getServletContext(),
request.getLocale(),model.asMap());
thymeleafViewResolver.getTemplateEngine().process("goods_list",ctx); return "goods_list";
@Autowired
private ThymeleafViewResolver thymeleafViewResolver;
WebContext ctx = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
// 手动渲染
html = thymeleafViewResolver.getTemplateEngine().process("这里写html页面名称", ctx);
springboot整合thymeleaf手动渲染的更多相关文章
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- 三、SpringBoot整合Thymeleaf视图
目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...
- SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...
- SpringBoot 整合thymeleaf
1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...
- SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图
在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...
- springboot整合thymeleaf+tiles示例
网上关于此框架的配置实在不多,因此想记录下来以防忘记 因为公司框架基于上述(公司采用gradle构建项目,楼主采用的是maven),所以楼主能少走些弯路: 1.创建springboot-maven项目 ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- SpringBoot学习9:springboot整合thymeleaf
1.创建maven项目,添加项目所需依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewo ...
随机推荐
- jQuery 对象与 Dom 对象转化
首先,我们需要知道,为什么我们需要转化两者,原因在于,两者提供的方法不能共用. 比如: $("#id").innerHTML; document.getElementById(id ...
- 设计模式四人帮(GOF)是什么?
1994年,有四位作者:Erich Gamma,Richard Helm,Ralph Johnson和John Vlissides发表了一本题为<设计模式 - 可重用的面向对象软件元素>的 ...
- 设置单网卡双ip
环境如下: [root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.7.1908 (Core) 安装时选择的最小化安装 -- ...
- 屏幕操作录制成gif图的技巧
我呢,在记录一些做过得实例的时候,总需要上一两张效果图,截静态图太浪费时间了,于是就找了一些录制git图的软件 一.Gif动画录制工具 这是我在360软件中心下载的,用了一下,不好用,录制出来的图是黑 ...
- 本地调试H5页面
摘要 详细讲述微信H5页面调试(安装在安卓或iOS手机上的),钉钉内H5页面的调试,QQ.微博以及各浏览器上H5页面的调试方法 背景 大学毕业快要一年了,用leader的话说我也是有一年开发经验的前端 ...
- JavaScript深浅拷贝区别
分享一篇自己关注的微信订阅号(前端大全)文章:JavaScript浅拷贝与深拷贝 作者:浪里行舟 https://github.com/ljianshu/Blog/issues/5 这里很详细的讲解了 ...
- docker Dockerfile学习---构建mongodb环境
1.创建项目目录并上传包 mkdir centos_mongodb cd centos_mongodb .tgz 2.编辑配置文件 vi mongodb.conf dbpath = /data/usr ...
- 数据转换--替换值(replace函数)
替换值 replace函数 data=Series([1,-999,2,-999,-1000,3]) data Out[34]: 0 1 1 -999 2 2 3 -999 4 -1000 5 3 d ...
- html input 上capture 参数在 安卓 苹果上的异同
安卓上 <input type="file" accept="image/*" capture="camera"> //只调用相 ...
- Promise 解决同步请求问题
在写小程序和vue项目中,由于 api 不提供 同步请求,因此,可以通过 Promise 来实现 同步请求操作 在这里 对于 Promise 不太了解的小伙伴 可以查找 Promise 的api 文 ...