前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >thymeleaf的常见问题汇总

thymeleaf的常见问题汇总

作者头像
Dream城堡
发布2018-09-10 12:46:45
9100
发布2018-09-10 12:46:45
举报
文章被收录于专栏:Spring相关Spring相关

thymeleaf的常见问题汇总

1.thymeleaf th:href 多个参数传递格式
代码语言:javascript
复制
  th:href="@{/Controller/update(param1=1,param2=${person.id})}"。就是使用逗号隔开多个参数!!!
thymeleaf的th:each常见用法
一.th:eath迭代集合用法:
代码语言:javascript
复制
<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布尔属性。

用法实例:

代码语言:javascript
复制
<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

代码语言:javascript
复制
@RequestMapping(value = "message", method = RequestMethod.GET)  
   public String messages(Model model) {  
           model.addAttribute("message", "hello");  
           return "index";  
   }  

2.not work

代码语言:javascript
复制
var m = ${message}; // not working  
alert(m);  

3.ok

代码语言:javascript
复制
<script th:inline="javascript">  
/*<![CDATA[*/  
  
    var message = [[${message}]];  
    console.log(message);  
  
/*]]>*/  
</script>  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • thymeleaf的常见问题汇总
    • 1.controller
      • 2.not work
        • 3.ok
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档