最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to loop through Java List<Object[]> object in JavaScript? - Stack Overflow

programmeradmin0浏览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.

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.

Share Improve this question edited Dec 10, 2013 at 16:04 Luiggi Mendoza 85.8k16 gold badges158 silver badges346 bronze badges asked Dec 10, 2013 at 15:56 Dinesh JDinesh J 7401 gold badge12 silver badges25 bronze badges 10
  • And in what language is this ? – adeneo Commented Dec 10, 2013 at 15:57
  • Read the generated source and you will see what the problem is. You need to use JSON. – SLaks Commented Dec 10, 2013 at 15:58
  • @adeneo The to part is Java (OP says JSP), the bottom part is JavaScript. – p.s.w.g Commented Dec 10, 2013 at 15:58
  • controller is java spring controller problem is with loop through my list in jsp page. – Dinesh J 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}? – p.s.w.g Commented Dec 10, 2013 at 16:00
 |  Show 5 more ments

2 Answers 2

Reset to default 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 .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.

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;

 });
发布评论

评论列表(0)

  1. 暂无评论