1:空对象判断:(dataResult为后台返回对象)
th:if="${dataResult}"
2:空 list 判断:(dataResultList为后台返回list对象)
th:if="${#lists.isEmpty(dataResultList)}"
3:th:each 循环:
<ul th:if="${not #lists.isEmpty(commonProblems)}"
th:each="item:${commonProblems}">
<li>
<p>
<span th:text="${item.question}"></span>
</p>
<p>
<span th:utext="${item.answer}"></span>
</p>
</li>
</ul>
用 th:text 不会解析html,用 th:utext 会解析html,在页面中显示相应的样式
4:th:onclick 传参
<a th:onclick="'javascript:consultClick(\''+${user.phone}+'\');'">
咨询
</a>
consultClick 为js中的定义的方法,接收一个参数
页面显示为:
<a onclick="javascript:consultClick('13088889999');">
咨询
</a>
5:th:if判断多个字段,使用 and 或 or
<p
th:if="${dataResult.startTime != '' and dataResult.endTime != ''}"
th:text="${dataResult.startTime} + ' 至 ' + ${dataResult.endTime}">
</p>
6:内联
<label for="body">Message body:</label>
<textarea id="body" name="body" th:inline="text">
Dear [[${customerName}]],
it is our sincere pleasure to congratulate your in your birthday:
Happy birthday [[${customerName}]]!!!
See you soon, [[${customerName}]].
Regards,
The Thymeleaf team
</textarea>
首先要在标签上声明:th:inline="text",然后赋值时使用两个中括号 [[${customerName}]]
[[...]] 等价于 th:text
[(...)] 等价于 th:utext
内联js:
<script type="text/javascript" th:inline="javascript">
var result = /*[[${dataResult}]]*/;
</script>
这样就将后台返回的 dataResult 数据赋值给了 result ;
th:inline ="javascript" 只能在html文件中使用
网友评论