一、first
通过这篇博文,您将了解到什么是Thymeleaf,以及和springboot作为伴侣她是如何使用的,同时还有一些简单的页面缓存的内容。
二、what is it
Thymeleaf是模板引擎,可以处理html,xml,js,css,甚至是纯文本(text),特别是html5。官网对他的解释是:是一个适用于web和独立环境的现代服务器端java模板引擎。
它可以完全替代JSP,她具有以下特点:
1. 支持html原形,在html标签里增加额外的属性来达到模板+数据的展示方式;浏览器解释html时会忽略未定义的标签属性,所以thymeleaf模板可以静态的运行;当有数据返回页面时,thymeleaf标签会动态地替换掉静态内容,使页面动态显示。
2. thymeleaf开箱即用,可以直接套用模板实现JSTL表达式效果,避免每天改模板,改jstl,比较优雅,同时开发人员可以扩展和创建自定义方言。
三、 using(+springboot)
3.1 添加依赖
和springboot一起使用,方便很多加入如下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.2 增加配置
在属性配置文件中可以增加如下配置:我们可以看到,thymeleaf使用的页面都是html格式的。
spring:
#thymeleaf
thymeleaf:
cache: false
servlet:
content-type: text/html
enabled: true
encoding: UTF-8
mode: HTML5
prefix: classpath:/templates/
suffix: .html
3.3 使用
在html的头部增加配置
<html xmlns:th="http://www.thymeleaf.org">
例如经典的welcom页面如下:
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">
<head>
<title>hello</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
</head>
<body>
<p th:text="'hello:'+${name}"></p>
</body>
</html>
在controller中我们的写法是,这事html中便可以获得${name}便是model中添加model的属性内容。关于更多的使用方式,请移步官网:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#what-is-thymeleaf
/**
* 页面跳转:hello頁面
* @return
*/
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model){
model.addAttribute("name","viola!");
return "hello";
}
三、 页面缓存
为什么要使用页面缓存呢,对于不经常变动的页面,我们可以设置一定的缓存时间,将其缓存起来(可以缓存在redis中);页面缓存又是什么呢?页面缓存的是整个页面,我们从redis中取出整个页面显示,是不是比每次加载会更快一些呢?下面是thymeleaf使用的页面渲染技术。操作的步骤是:1.取缓存,2.手动渲染,3.输出结果。
@Resource
ThymeleafViewResolver thymeleafViewResolver;
/**
* 商品列表页面
* 优化:页面缓存
* @return
*/
@GetMapping(value = "/goods/to_list",produces = "text/html")
@ResponseBody
public String goods(HttpServletRequest request, HttpServletResponse response,Model model, MiaoShaUser user){
model.addAttribute("user",user);
//页面缓存
//取缓存
String html=redisService.get(GoodsKey.getGoodsList,"",String.class);
//redis中有:直接返回使用
if (!StringUtils.isEmpty(html)){
return html;
}
//redis中没有:存
//商品信息
List<GoodsVo> goodsList=goodsService.listGoodsVo();
model.addAttribute("goodsList",goodsList);
//手动渲染
WebContext cxt=new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
html=thymeleafViewResolver.getTemplateEngine().process("goods_list",cxt);
if (!StringUtils.isEmpty(html)){
redisService.set(GoodsKey.getGoodsList,"",html);
}
return html;
}
四、 静态页面
用thymeleaf可以实现动态页面和静态页面,通俗的讲静态页面便是:html+ajax,动态页面在thymeleaf中是如何体现呢?我认为是通过$从服务器端取值,类似这样的:
<td colspan="3" th:text="${goods.stockCount}"></td>
之前也接触过很多动态页面,比如说:jsp,aspx,这些都是动态页面资源。那么静态页面资源和动态页面资源有什么不同呢?
静态页面:客户端使用web浏览器(IE等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服务器我现在需要哪个页面,所有的请求交给web服务器,之后web服务器根据用户需要,从文件系统(存放了所有静态页面的磁盘)取出内容。之后通过web服务器返回给客户端,客户端收到内容之后经过浏览器渲染解析,得到显示的效果。
动态页面:客户端使用web浏览器(IE等)经过网络连接到服务器上,使用http协议发送到请求(request)到服务器端,此时需要将请求转接到WEB Container(WEB容器),在web container会连接数据库,从数据库中取出数据等一系列操作后拼凑页面的展示内容,把所有展示的内容交给web服务器,之后通过web服务器将内容发送给客户端浏览器进行解析执行。
我们可以接着分析一下jsp动态页面请求的过程,看似直接访问的是jsp页面,其实是先到服务器端,服务器创建好req和resp对象后再给jsp使用。jsp中完成设置字符集合表单参数后再调用servlet,完成业务处理。然后返回jsp,jsp会生成相应的html页面。该页面会返回到服务器,再由服务器,通过response对象返回给客户端。
四、 结语
不断了解,不断精进~、
感谢您宝贵的阅读时间~