您现在的位置是:首页 > 文章详情

thymeleaf的常见问题汇总

日期:2018-08-20点击:600

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> 
原文链接:https://yq.aliyun.com/articles/652827
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章