页面HTML引入thymeleaf
在html上面添加
<html lang="en" xmlns:th="http://www.thymeleaf.org">
th:text 和th:utext
例如:传入页面的sh数据为:
User user = new User("<span style='color:blue;'>张三</span>",23);
页面
th:text:<span th:text="${user.name}"></span><br/>
th:utext:<span th:utext="${user.name}"></span><br/>
结果:
th:text会把传入的内容全部展示出来。但th:utext会把传入的内容转换为html元素解析。
th:object
传入页面的数据为:
User user = new User("张三",23);
页面:有两种写法都可以用
<form th:object="${user}">
姓名:<input th:field="*{name}"><br/>
年龄:<input th:field="*{age}"><br>
</form>
<div th:object="${user}">
姓名:<input th:value="*{name}"><br/>
年龄:<input th:value="*{age}"><br>
</div>
结果:
日期函数:
传入的数据:
User user = new User("张三",23);
user.setDate(new Date());
页面:
<div th:object="${user}">
姓名:<input th:value="*{name}"><br/>
年龄:<input th:value="*{age}"><br>
日期:<input th:value="*{#dates.format(date,'yyyy*MM*dd')}"/>
</div>
结果:
但不可以这么写
<input th:field="*{#dates.format(date,'yyyy|MM|dd')}"/>
原因我个人觉得,你让id和name属性等于这么一串是不是不大合适
th:href 和th:src以及th:action
这两个都是类似的用法即用@{}包含请求的路径即可
<a th:href="@{www.baidu.com}"></a>
<img th:src="@{/img/1.jpg}">
<script th:src="@{/js/test.js}"></script>
<form th:action="@{/路径}">
th:if
传入页面的数据为:
User user = new User("张三",23);
页面:
<span th:if="${user.age} == 23">等于</span>
<span th:if="${user.age} gt 23">大于</span>
<span th:if="${user.age < 23}">小于</span>
结果:
传入
User user = new User("张三",25);
结果
传入:
User user = new User("张三",18);
结果:
比较类似于el表达式,在括号里面外面判断都可以。