thymeleaf的常见问题汇总

时间:2023-03-09 03:16:53
thymeleaf的常见问题汇总

thymeleaf的常见问题汇总

1.thymeleaf th:href 多个参数传递格式

  th:href="@{/Controller/update(param1=1,param2=${person.id})}"。就是使用逗号隔开多个参数!!!

thymeleaf的th:each常见用法

一.th:eath迭代集合用法:

<table border="1" id="stuTable">
<tr>
<td>是否选中</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr th:each="stu,userStat:${studentList}" >
<td><input th:type="checkbox" th:name="id" th:value="${stu.id}"></td>
<td th:text="${stu.id}">编号</td>
<td th:text="${stu.name}">姓名</td>
<td th:text="${stu.age}">年龄</td>
</tr>
</table>

二.迭代下标变量用法:

  状态变量定义在一个th:每个属性和包含以下数据:

  1.当前迭代索引,从0开始。这是索引属性。index

  2.当前迭代索引,从1开始。这是统计属性。count

  3.元素的总量迭代变量。这是大小属性。 size 

  4.iter变量为每个迭代。这是目前的财产。 current 

  5.是否当前迭代是奇数还是偶数。这些even/odd的布尔属性。  

  6.是否第一个当前迭代。这是first布尔属性。  

  7.是否最后一个当前迭代。这是last布尔属性。

用法实例:

<table border="1" id="stuTable">
<tr>
<td>是否选中</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr th:each="stu,userStat:${studentList}" th:class="${userStat.odd}?'odd':'even'">
<td th:text="${userStat.index}"></td>
<td><input th:type="checkbox" th:name="id" th:value="${stu.id}"></td>
<td th:text="${stu.id}">编号</td>
<td th:text="${stu.name}">姓名</td>
<td th:text="${stu.age}">年龄</td>
</tr>
</table>

3.thymeleaf 传递数据到js变量

如何把控制器传来的model中的值传递给js变量呢?

需要以下两个:

  • <script th:inline="javascript">
  • var message = [[${message}]]

1.controller

@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("message", "hello");
return "index";
}

2.not work

var m = ${message}; // not working
alert(m);

3.ok

<script th:inline="javascript">
/*<![CDATA[*/ var message = [[${message}]];
console.log(message); /*]]>*/
</script>