-1

I have a bean something like this which returns a json Object (mappedData) to jsp.

 final Map<String, TreeMap<String, List<String>>> exampleMap = wFAServiceProxy .fetch();        
 mappedData.put("exampleMap ", exampleMap );
 retrun mappedData;

I also have jsp where I need to fetch the exampleMap and display the keys. so I am doing the following.

<c:forEach items='${serviceDetailFormData.get("exampleMap ")}' var="category">
      <a:dropdownOption value="${category.key}">${category.key} </a:dropdownOption>
</c:forEach>

but I am facing this problem. I searched a lot but couldn't find a solution.

Caused by: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:308)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:272)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:189)
at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:287)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_c_005fforEach_005f1(serviceDetail_jsp.java:3152)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_a_005fdropdown_005f1(serviceDetail_jsp.java:3094)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_a_005fgridColumn_005f32(serviceDetail_jsp.java:3046)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_a_005fgridRow_005f9(serviceDetail_jsp.java:2926)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_a_005fform_005f0(serviceDetail_jsp.java:662)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_a_005fgridColumn_005f1(serviceDetail_jsp.java:488)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_c_005fif_005f0(serviceDetail_jsp.java:435)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_a_005fgridRow_005f0(serviceDetail_jsp.java:357)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspx_meth_a_005fbody_005f0(serviceDetail_jsp.java:238)
at org.apache.jsp.WEB_002dINF.views.serviceDetail_jsp._jspService(serviceDetail_jsp.java:188)

If i print the exampleMap thorugh jsp. It shows this

{"cws":{"flipkart":["amazon","coral","Download Problem","upload","operation","Security","Upload Problem"]},"central Mall":{"Tools":["Flipkart- Document Management System"]}} 

but I want to iterate over it. can anyone help please.

Abhinav
  • 103
  • 1
  • 12
  • Possible duplicate of [How to loop through a HashMap in JSP?](http://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp) – kryger Oct 06 '15 at 10:02

3 Answers3

1

The following solved my problem

<c:forEach items='${serviceDetailFormData..get("exampleMap ").keys()}' var="category">
  <a:dropdownOption value="${category.key}">${category.key} </a:dropdownOption>
</c:forEach>
Abhinav
  • 103
  • 1
  • 12
0

Like in Java, you have to call entrySet() to iterate the map entries:

<c:forEach items='${serviceDetailFormData.get("exampleMap ").entrySet()}' var="category">
      <a:dropdownOption value="${category.key}">${category.key} </a:dropdownOption>
</c:forEach>

Alternatively, if the JSP doesn't need to do map lookups, put the entry set into the data:

mappedData.put("exampleMap ", exampleMap.entrySet() );
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Hello @Andreas, I am still getting the same problem. – Abhinav Oct 06 '15 at 04:35
  • This the new error. Caused by: org.apache.jasper.el.JspELException: /WEB-INF/views/serviceDetail.jsp(165,37) '${serviceDetailFormData.get(" exampleMap ").entrySet()}' Method not found: class org.json.JSONObject.entrySet() – Abhinav Oct 06 '15 at 04:38
  • If you're putting a `Map` into the data, why is it then a `JSONObject` when you take it back out? – Andreas Oct 06 '15 at 04:45
  • I have no Idea, Why it is giving JSONObject.I am putting map in JSONObjecct but when I retrieve map, its becoming a json object.The problem solved by using keys() instead of entrySet().Thanks a ton Andreas. your answer helped me to unblock my self. – Abhinav Oct 06 '15 at 04:52
0
<c:forEach items='${serviceDetailFormData.get()}' var="category">
      <a:dropdownOption value="${category.key}">${category.key} </a:dropdownOption>
</c:forEach>

instead of

<c:forEach items='${serviceDetailFormData.get("exampleMap ")}' var="category">
      <a:dropdownOption value="${category.key}">${category.key} </a:dropdownOption>
</c:forEach>
Sanjay Singh Rawat
  • 1,849
  • 2
  • 16
  • 24