thymeleaf使用th:inline属性将后台绑定的数据传给js变量

thymeleaf可以直接将从后台获取的数据传给js中使用。

  1. 后台接口示例:
1
2
3
4
5
6
7
8
9
@Controller
public class TestController  {

    @GetMapping("test")
    public String test(Model model) {
            model.addAttribute("test", "hello"); // 将"hello"赋值给键名为test的属性
            return "index";
    }
}
  1. js代码:
1
2
3
4
<script th:inline="javascript">
    var test = [[${test}]];
    console.log(test);
</script>

原文链接:
https://www.cnblogs.com/kingsonfu/p/10272925.html

供参考文章链接:
https://www.cnblogs.com/wwct/p/12143084.html
https://blog.csdn.net/u013848401/article/details/78601672
https://blog.csdn.net/qq_22860341/article/details/84646193