thymeleaf使用方法记录

//头部声明
<html xmlns:th="http://www.thymeleaf.org">

//文本显示,使用 ${后端传过来的变量}
<p class="book_inline b" th:text="${book.bookAuthor}"></p>

//在超链接中使用@{},里面是url,带参数的话使用(),括号里面是参数,多个参数用逗号隔开。thymeleaf会自动识别添加springboot配置文件中context-path参数并自动添加到链接前,所以url前不用手动添加项目路径
<a th:href="@{/queryBookById(id=${book.id},pageNo=${bookpage.pageNo})}"><span th:text="${book.bookName}"></span></a>

//调用js函数,onclick的值中,除了${},其余都是字符串,需要使用引号将函数名,函数的括号以及参数列表中的逗号引起来。
<a class="book_buy page" th:onclick="'addToCart('+'\''+${book.id}+'\''+','+${bookpage.pageNo}+')'">加入购物车</a>


//关于list的遍历
<tr th:each="shopCar:${shopCars}">
    <td th:text="${shopCar.bookName}"></td>
    <td th:text="${shopCar.price}"></td>
</tr>

//thymeleaf在js中的使用
<script type="text/javascript" th:inline="javascript">
    var xArr = [[${xArr}]];
<script>

猜你喜欢

转载自blog.csdn.net/u010503427/article/details/81096460