I want to access a model attribute in Javascript. I use the following code:
model.addAttribute(\"data\", responseDTO);
My DTO class:
So I just implemented a similar solution to Grant's first option with a List of objects, but used the Gson library to convert the object to a JSON string, then used JSON.parse() to turn it into a javascript object:
On the server:
List foo = database.getCustomObjects();
model.addAttribute("foo", new Gson().toJson(foo));
In the page javascript:
var customObjectList = JSON.parse('${foo}');
console.log(customObjectList);
Notice that when I reference the model object foo, that I do so as a string '${foo}'. I believe you are getting your error because you reference it outside of a string. So the correct code would be:
var data = eval('('+ '${dataJson}' +')');