Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over hashmap in JSP in struts application

Tags:

java

struts-1

I have a HashMap object that I am getting on a JSP page.

HashMap<Integer,Gift_product> gift_hm = new HashMap<Integer,Gift_product>();
gift_hm.put(17,new Gift_product("doll",67));

Now I need to iterate this and display content on JSP. The Gift_product class contains two fields: name and price.

JSP output should be

serial no.           product name     price
17                    Doll            67

How can I achieve it?

like image 407
Pedantic Avatar asked Jun 15 '10 10:06

Pedantic


2 Answers

This one works for me (struts2):

<s:iterator value="giftMap" var="giftMapElement">
    <s:set var="giftKey" value="#giftMapElement.key"/>
    <s:set var="giftValue" value="#giftMapElement.value"/>
    <tr>
        <td><s:property value="#giftKey"/></td>
        <td><s:property value="#giftValue.productName"/></td>
        <td><s:property value="#giftValue.price"/></td>
    </tr>
</s:iterator> 
like image 165
user007 Avatar answered Nov 15 '22 11:11

user007


Solution
-----------
<s:iterator value="map">
  <h3><s:property value="key" /></h3>
  <table>
  <s:iterator value="value">
    <tr><td><s:property /></td></tr>
  </s:iterator>
  </table>
</s:iterator>
like image 26
user2564182 Avatar answered Nov 15 '22 10:11

user2564182