thymeleaf 传递数据到js变量

来源:互联网 发布:淘宝同款比价软件 编辑:程序博客网 时间:2024/06/09 20:37

如何把控制器传来的model中的值传递给js变量呢?

下面便会找到答案...


1.controller

[java] view plain copy
  1. @RequestMapping(value = "message", method = RequestMethod.GET)  
  2.    public String messages(Model model) {  
  3.            model.addAttribute("message""hello");  
  4.            return "index";  
  5.    }  


2.not work

[javascript] view plain copy
  1. var m = ${message}; // not working  
  2. alert(m);  


3.ok

[javascript] view plain copy
  1. <script th:inline="javascript">  
  2. /*<![CDATA[*/  
  3.   
  4.     var message = [[${message}]];  
  5.     console.log(message);  
  6.   
  7. /*]]>*/  
  8. </script> 

原创粉丝点击