js与thymeleaf结合,js获取thymeleaf变量

前段页面代码

<tr th:each="book : ${books}">
            <td th:text="${book.author}"></td>
            <td th:text="${book.title}"></td>
            <td th:text="${book.url}"></td>
            <script th:inline="javascript">//一定要加上这句

                function localRefresh() {
                    // 装载局部刷新返回的页面
                    $('#table_refresh2').load("/local/"+[[${book.ID}]]);//这里直接可以使用thymeleaf中的变量,通过内联样式
                }
            </script>
        </tr>

最主要就是加上thymeleaf与js结合的语句,常见的thymeleaf内联语句有三种

<script th:inline="javascript"></script>
用于js的
<span th:text="${session.user.name}">Sebastian</span>
用于普通html,任意位置都可以。默认也可以省略不写,thymeleaf也是支持的。
<body th:inline="text">
  <p>Hello, [[${session.user.name}]]!</p>
</body>
直接用于父标签,子标签可以直接使用

后端代码示例

@Controller
@RequestMapping("/refresh")
public class IndexController {

  @RequestMapping
  public String globalRefresh(Model model) {
      List<Map<String,String>> lists = new ArrayList<>();
      Map<String,String> map = new HashMap<>();
      map.put("author", "曹雪芹");
      map.put("title", "《红楼梦》");
      map.put("url", "www.baidu.com");
      map.put("ID","2");
      lists.add(map);
      model.addAttribute("books", lists);
   
      return "test";
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值