我正在尝试从js和html访问Spring Model属性。
我已经按照下面的链接尝试了所说的一切,但我一直收到错误:
How to access model attribute in Javascript
这是我的代码:
MyVOClass.java
private String id;
private String name;
//getters setters here
MyController.java
@RequestMapping(value = "/Display", method = RequestMethod.GET, headers
= "Accept=text/html")
public String Display(@RequestParam(value = "id", required = true)
String id, @RequestParam(value = "name", required = true) String name,
Model model) {
//set the values to the VO Object
MyVOClass myVo = call to another class to set the values
model.addAttribute("myVo ", new Gson().toJson(myVo));
return "html page name ";
Js file:
var ID = JSON.parse('${myVo.id}');
我在控制台上收到以下错误:
angular.js:14525 SyntaxError: Unexpected token $ in JSON at position 0
at JSON.parse (<anonymous>)
编辑2:
如果我尝试
var ID = JSON.parse(${myVo.id});
我收到以下错误:
Uncaught SyntaxError: missing ) after argument list
Uncaught Error: [$injector:modulerr]
我不确定我错过了什么?我需要在我的js和html中使用任何其他配置来访问Model吗?
编辑1:
添加我的Java API代码:
@RequestMapping(value = "/display", method = RequestMethod.GET, headers
= "Accept=text/html")
public String display(@RequestParam(value = "param1", required = true)
String param1, Model model) {
String html = "/view";
MyVo myVo = new MyVo();
//set data
Gson gson = new Gson();
String respJson = gson.toJson(authresp);
String respstr = authrespJson.toString();
model.addAttribute("myVo ", respstr );
System.out.println("response Json string "+respstr );
return html;
}