我正在使用Thymeleaf作为模板引擎。如何将变量从Spring模型传递给JavaScript变量?
Spring-side:
@RequestMapping(value = "message", method = RequestMethod.GET) public String messages(Model model) { model.addAttribute("message", "hello"); return "index"; }
Client-side:
<script> .... var m = ${message}; // not working alert(m); ... </script>
根据官方文件:
<script th:inline="javascript"> /*<![CDATA[*/ var message = /*[[${message}]]*/ 'default'; console.log(message); /*]]>*/ </script>