0

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.

10
  • And in what language is this ? Commented Dec 10, 2013 at 15:57
  • Read the generated source and you will see what the problem is. You need to use JSON. Commented Dec 10, 2013 at 15:58
  • @adeneo The to part is Java (OP says JSP), the bottom part is JavaScript. Commented Dec 10, 2013 at 15:58
  • controller is java spring controller problem is with loop through my list in jsp page. Commented Dec 10, 2013 at 15:58
  • 1
    I have never used JSP, but just a guess: what if you remove the quotes around the variable, e.g. var allpermissionList = ${permissionList}? Commented Dec 10, 2013 at 16:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

I got resolved .. thanks all.

Controller

List<Object[]> permissionList = new ArrayList();
// load values
List<Map<String, String>> listAll = new ArrayList<>();
   if (permissionList != null && permissionList.size() > 0) {
       for (Object[] objects : allPermissions) {
           Map map = new HashMap();
           map.put("id",objects[0]);
           map.put("permName", objects[1]);
           listAll.add(map);
        }
    }

ObjectMapper mapper = new ObjectMapper();
String permissionList = mapper.writeValueAsString(listAll);
model.addAttribute("permissionList", permissionList);

Jsp

 var allpermissionList = eval(${permissionList});
  $.each(allpermissionList, function (index, permission) {
     var id = permission.id;
     var name = permission.name;

 });

Comments

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.