1

I generate the json object in spring controller class like below

My Controller Class:
            Map<String,Object> model=new HashMap<String,Object>();
             model.put("project", project);
             model.put("roles", userService.dispRoles());
            for(int i=0;i<phaseList.size();i++){

                        TblProjectPhase projectPhase=phaseList.get(i);
                         try {
                            json=new JSONObject();
                            json.put("phasename", projectPhase.getPhaseName());
                            json.put("remain", getRemainingResource(projectPhase));
                                json.put("users",sessionfactory.getCurrentSession().createCriteria(TblProjectResource.class)
                    .add(Restrictions.eq("tblProjectPhase",phase))
                    .list());
                            ja.put(json);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    JSONObject res=new JSONObject();
                    res.put("result", ja);
             model.put("userList",res );

and my json object looks like below

{"result":[
{"phasename":"BVFD Phase1",
"remain":0,
"users":
["TblProjectResource@1cb5e384",
"TblProjectResource@1cb5e385"]
}
]}

i tried to iterate this in my jstl core like below

<c:choose>
                             <c:when test="${not empty model.userList }">
                             <c:forEach var="user" items="${model.userList}">
                             <c:forEach var="child" items="${user.result}">
                             <c:out value="${child.phasename}"/>
                              <c:forEach items="child.users" var="u">
                               <c:out value="u.name"/>
                             </c:forEach>
                             </c:forEach></c:when>
                             <c:otherwise>No Users Availble In this Phase</c:otherwise></c:choose>

but my code throws exception in browser.The exception is

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;

How to iterate the coorect way in my json using jstl? Any Help will be greatly appreciated!!!

Selva
  • 1,620
  • 3
  • 33
  • 63

1 Answers1

1

<c:forEach> tag can iterate only a Collection, Map, Iterator, Enumeration or String. Anything else can't be iterated by <c:forEach>.

As userList in the request is of JSONObject type you can't iterate it using jstl . you can rather try with javascript to iterate.

Addional Info and Work arounds here

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56