美文网首页
Thymeleaf常用th标签表达式

Thymeleaf常用th标签表达式

作者: _灯火阑珊处 | 来源:发表于2019-08-14 11:23 被阅读0次
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判断多个字段,使用 andor
<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文件中使用

相关文章

网友评论

      本文标题:Thymeleaf常用th标签表达式

      本文链接:https://www.haomeiwen.com/subject/rnmvjctx.html