1、拼接url
1 | <img th:src="@{/img/research/{filename}(filename=${research.filename})}"> |
1 | <a th:href="@{'https://'+${url.url}}" th:text="${url.urlName}"></a> |
2、在js中获取项目的路径
1 | <script th:inline="javascript"> |
3、获取session、model中的值
1 | <script th:inline="javascript"> |
3、将公共界面抽离出来
其中th:fragment=”header” 表示下面的代码是公共部分
1 | <div style="min-width:500px;" th:fragment="header"> |
在其他界面上引用,代码如下
1 | <div th:replace="~{index :: header}"></d |
1 | 元素导入: |
模板名就是html的路径,默认去templates文件夹下找,这个路径不用加.html,在配置文件中配置过了。
4、If和往Js函数中传递参数
如果confirmDelete()函数需要字符串id ,请尝试这个
1 | th:onclick="'javascript:confirmDelete(\'' + ${company.id} + '\');'" |
如果它需要一个数字ID
1 | th:onclick="'javascript:confirmDelete(' + ${company.id} + ');'" |
5、 转义和非转义的 html 文本
1 | <h2>显示 转义和非转义的 html 文本</h2> |
6、显示对象以及对象属性
1 | <div class="showing"> |
7、循环遍历
1 | List<Product> ps = new ArrayList<>(); |
8、结合select、input
1 | <select size="3"> |
1 | <input name="product" type="radio" th:each="p:${ps}" th:value="${p.id}" th:checked="${p.id==currentProduct.id}" th:text="${p.name}" /> |
9、格式化日期
1 | 直接输出日期 ${now}: |
10、判断集合是否为空
1 | th:if="${not #lists.isEmpty(集合名称)}"> |