Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  101] [ 3]  / answers: 1 / hits: 12285  / 11 Years ago, tue, december 10, 2013, 12:00:00

Controller



List<Object[]> permissionList = new ArrayList();
//fill permissionList with list of object ayyays (objetct[0] = permission id, object[1] = permission)
model.addAttribute(permissionList, permissionList);


jsp



var allpermissionList = ${permissionList};

$.each(allpermissionList, function (index, av) {
var id = av[0];
vat name=av[1];
//set values to div element
});


i can't loop through my list with js $each()... av[0] and av[1] cannot cannot obtain.


More From » java

 Answers
4

Java code runs on the server. JavaScript runs on the client. They are very different languages and do not interoperate out of the box. When you need to pass data from Java to JavaScript, the easiest is to serialize it to JSON using Jackson for example.



import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper om = new ObjectMapper();
model.addAttribute(permissionList, om.writeValueAsString(permissionList));


and in the JSP:



var allpermissionList = ${permissionList};


Note that not all Java object are serializable to JSON so the objects in your list should be simple Java types (String, Number...) or POJOs.


[#49667] Monday, December 9, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckaylab

Total Points: 311
Total Questions: 120
Total Answers: 93

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
;