I am trying to access a Spring Model attribute from js and html.
I have followed the below link and tried everything it said, but I keep getting errors:
How to access model attribute in Javascript
This is the code which I have:
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}');
I am getting the below error on the console:
angular.js:14525 SyntaxError: Unexpected token $ in JSON at position 0
at JSON.parse (<anonymous>)
EDIT 2:
If I try
var ID = JSON.parse(${myVo.id});
I get the following error:
Uncaught SyntaxError: missing ) after argument list
Uncaught Error: [$injector:modulerr]
Im not sure what am I missing? Do i need any additional config in my js and html to access Model?
EDIT 1:
Adding my Java API code as well:
@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;
}