0

I am no expert in JSP technology and looking for some help here to access HashMap from bean. I have a java class which returns a Hashmap, and would like to access both key and values of hash map in JSP. The following is what I tried

//jsp:
<jsp:useBean id="mc" class="MyMapClassReturnsMap" /> 
<c:forEach items="${mc.getMap()}" var="mapkeyval">
<tr>
<td><c:out value="${mapkeyval.key}"></c:out></td>
</tr>
</c:forEach>

//Error:
javax.el.PropertyNotFoundException: Property 'key' not found on type java.lang.String

page returning the map as String, if I remove .key from mapkeyval page displaying map as String. Not sure what I am missing, but any pointers would be appreciated gr8ly

I am sure I am returning HashMap from my class, following is my main method, which works as expected

 HashMap<String, String> jname1 = new HashMap<String, String>();
  ..
  ..
 public static void main(String[] args) {
 MyMapClassReturnsMap ta = new MyMapClassReturnsMap();
 ta.searchFiles("root","CUST");
 for(Map.Entry<String, String> s: ta.jname1.entrySet())
 System.out.println("HashMap Values : "+ s.getKey() + " :" +s.getValue());
    }
  • And you're 100% sure that you're returning a `Map` from `mc.map`? The [canonical answer](http://stackoverflow.com/q/2210907/1079354) is that your code should "just worK", if you're iterating over a `Map`. – Makoto May 18 '14 at 04:15
  • Just updated the post with my test main method, which works as expected – user2888060 May 18 '14 at 04:47
  • 2
    JSP: mc.getMap(); main: ta.jname1. Is it really same? Why do you not use ta.getMap() in main as well to verify that it works? – Leos Literak May 18 '14 at 05:34

1 Answers1

0

Try this,

<c:forEach items="${jname1 }" var="mapkeyval">

and provide the getter and setter methods for 'jname1' in your class.

gang010
  • 3
  • 3