thymeleaf 传递数据到js变量

1.controller

@RequestMapping(value = "message", method = RequestMethod.GET)  
   public String messages(Model model) {  
           model.addAttribute("message", "hello");  
           return "index";  
   } 

2.该方法不行

var m = ${message}; // not working  
alert(m);  

3.可行方法

<script th:inline="JavaScript">  
/*<![CDATA[*/  
  
    var message = [[${message}]];  
    console.log(message);  
  
/*]]>*/  
</script>  

推荐阅读更多精彩内容