springboot之thymeleaf入门
由于JSP是不能在压缩包内解析的,所以spring boot默认不支持JSP的。因此我们需要使用第三方的模板引擎,比如我们这次介绍的thymeleaf
官网地址:https://www.thymeleaf.org/
什么是thymeleaf
thymeleaf是一个现代Java服务端的模板引擎,语法简单,和JSP类似。但性能一般,不适合用于高并发的场景。
使用thymeleaf
-
在
pom.xml
文件里,导入spring boot的thymeleaf启动器依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
-
在
resources/templates
创建一个index.html
,内容如下,注意需要引入th命名空间<!DOCTYPE html> <!-- 引入th命名空间 --> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!-- 获取model中的content,作为标签内容,会覆盖掉默认的内容 --> <h1 th:text="${content}">你好</h1> </body> </html>
-
写一个controller,当浏览器访问该controller时,返回
index
字符串,spring boot会帮我们找到对应的页面返回的@Controller public class ViewController { @GetMapping("/") public String index(Model model){ model.addAttribute("content", "你们好啊"); return "index"; } }
-
访问http://127.0.0.1:8080/,就可以看到“你们好啊”
tips:spring boot若使用了模板引擎,则视图解析器要返回的页面必须放在resources/templates中,若没有使用模板引擎,页面必须放在resources/static中,当作静态资源访问
thymeleaf语法
表达式
表达式名字 | 语法 | 用途 |
---|---|---|
变量取值 | ${} | 获取请求域、session、对象等 |
选择变量 | *{} | 获取上下文对象值 |
消息 | #{} | 获取国际化等值 |
连接 | @{} | 转成链接,会拼上项目访问路径 |
片段表达式 | ~{} | 引入公共页面 |
字面量
- 文本值:"one text’, ‘Another onel!’…
- 数字:0.34
- 布尔值:true, false
- 空值:nulll
- 变量:one, two, 变量不能有空格
运算符
数学运算:+ - * / %
布尔运算:and or ! not
比较运算:> < >= <= == !=
条件运算:
If-then: (if) ? (then)
If-then-else: (if) ? (then): (else)
Default: (value) ?: (defaultvalue)
设置属性值
有如下两种写法
<input type="text" th:value="${defaultValue}">
<input type="text" th:attr="value=${defaultValue}">
设置标签内容
有如下两种写法
<h1>[[${content}]]</h1>
<h1 th:text="${content}"></h1>
若需要拼接字符串,写法分别如下
<h1>[[${content}]]拼接内容</h1>
<h1 th:text="${content}+拼接内容"></h1>
引入公共组件
我们开发的页面往往会有些公共的组件,如jquery,导航栏等。如果我们在每个页面中都写上,当我们需要修改时会非常麻烦,且冗余严重。我们可以将公共的组件写在单独的页面中,其他页面需要时引入即可。
假设我们有个版权声明在所有页面都需要使用,我们可以把版权声明写在footer.html
,如下
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
</body>
</html>
在需要使用的地方使用引入即可,如下
<body>
<div th:insert="footer :: copy"></div>
</body>
其中:
th:fragment
:表示该组件的名称th:insert
:表示要引入的组件,格式为模板名 :: 组件
,其中组件既可以使用th:fragment
所指定的组件名,也可以使用选择器,如footer :: #copy
等。
和th:insert
类似的还有th:replace
和th:include
(th:include
在3.0后不推荐使用),他们引入的效果不同,例如当你使用这三者引入时,如下:
<body>
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
</body>
效果为
<body>
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
<div>
© 2011 The Good Thymes Virtual Grocery
</div>
</body>
循环遍历
当我们要根据对象生成列表时,需要使用循环th:each
,具体用法如下
<table>
<thead>
<tr>
<th>#</th>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody>
<!-- 遍历users,将其中每个元素放到user中,每次遍历的状态放到stats中 -->
<tr th:each="user,stats:${users}">
<!--序号,count从1开始,index是从0开始的-->
<td th:text="${stats.count}"></td>
<!--获取遍历对象的属性-->
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
</tr>
</tbody>
</table>