Thymeleaf是什么?
- 他是一种服务端的Java模板引擎,类似于Jsp这种。
基本语法
常用表达式
- ${…}:获取请求与Session域,和对象
- @{…}:可以理解为字符串拼接,用于生成连接
- ~{…}:片段表达式,用于引入公共页面
用于设置值
<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">
迭代
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
Thymeleaf的使用
使用步骤
1. 引入场景
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. 场景默认自动配置好了
- templacement用于放置模板页面
- static用于放置CSS、js等资源

3. 在HTML标签
- 放置
xmlns:th="http://www.thymeleaf.org

最后事项
- 需要注意的是,thymeleaf当中的页面不能直接访问,需要编写控制器,写访问方法。
