thymeleaf 将后端绑定数据直接传递js变量

根据自我需求,thymeleaf可以直接将后端数据传递给js中进行使用,例如:

1.后端接口数据:

@Controller
public
class TestController { @RequestMapping(value = "test", method = RequestMethod.GET) public String test(Model model) { model.addAttribute("test", "hello"); return "index"; } }

利用thymeleaf将其传递给js使用:

<script th:inline="javascript">

    var test= [[${test}]];
    console.log(test);

</script>

注:[[…]]之间的内容可以被赋值。为了使其生效,必须在此标签或者任何父标签上有th:inline属性。此属性有三种值(text , javascript and none)

posted @ 2019-01-15 16:59  花拾夕  阅读(14454)  评论(0编辑  收藏  举报