2014-10-30 50 views
1

迭代JSON值I产生在弹簧控制器类JSON对象像下面异常使用JSTL芯

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); 

和我的JSON对象看起来像下面

{"result":[ 
{"phasename":"BVFD Phase1", 
"remain":0, 
"users": 
["[email protected]", 
"[email protected]"] 
} 
]} 

我试图在遍历这个我像下面的jstl核心

<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> 

但我的代码在浏览器中抛出异常。例外是

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

如何使用jstl在我的json中迭代coorect方式? 任何帮助将不胜感激!

+1

JSTL和JSP EL不能使用JSON。他们使用Java bean和集合。 – 2014-10-30 07:13:31

+0

感谢您的解释 – Madhesh 2014-10-30 07:15:11

回答

1

<c:forEach>标签可以遍历只有CollectionMapIteratorEnumerationString。其他任何东西都不能被<c:forEach>迭代。

由于userList中的请求是JSONObject类型,您不能使用jstl来迭代它。您可以尝试使用javascript进行迭代。

Addional Info and Work arounds这里