0

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;
    }
16
  • Could you add the JSON contents of ${myVo.id} to your post please? Commented Oct 19, 2017 at 23:20
  • Hi @admcfajn, how I can do it? since the exception gets thrown on that line. Commented Oct 20, 2017 at 17:37
  • In JS: console.log(${myVo.id}); the right-click inspect-element and view the console. Not sure how to do it in Java. You'll probably want to print/echo/output it in Java before it gets to the js as the problem could be happening in the Java code, not the JavaScript code. Commented Oct 20, 2017 at 17:59
  • you'll also want to comment out the line which is throwing the exception while you inspect the json. Commented Oct 20, 2017 at 19:04
  • From Java, the response is populated properly. In the JS end If I try console.log(${myVo.id}); it throws an error Unexpected Token in the js end. Commented Oct 20, 2017 at 19:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.