9

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?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Pedantic
  • 1,368
  • 10
  • 39
  • 70
  • Funny ... , I did not add the generics part in the source (I just reformatted into code) but the revision history seems to disagree in this? – rsp Jun 15 '10 at 11:01
  • @rsp: sometimes generics are interpreted as html tags and cut out of the formatted output. Formatting the post correctly as code then makes them show up. Use "View source" on the revision to verify this. – Joachim Sauer Jun 15 '10 at 11:16

4 Answers4

12

Check out the struts <logic:iterate> tag. When iterating over a HashMap, each entry is a java.util.Map.Entry, to get the key (in this example the serial number) and value (the Gift_product object) out use the key and value properties like this:

First set the HashSet as an attribute in your action class e.g. request.setAttribute("gift_hm", gift_hm); and then in the jsp:

<logic:iterate id="mapEntry" name="gift_hm">
  <bean:define id="gift" name="mapEntry" property="value">
  <tr>
    <td><bean:write name="mapEntry" property="key"></td>
    <td><bean:write name="gift" property="productName"></td>
    <td><bean:write name="gift" property="price"></td>
  </tr>
</logic:iterate>
krock
  • 28,904
  • 13
  • 79
  • 85
  • is it directly get it serial no which is in key part, product name which is in value.getProdutname() and price value.getPrice()... not quite sure.. – Pedantic Jun 15 '10 at 11:32
  • I revised it to print the HashMap key (mapEntry.getKey) as the serial number and pull the Gift_product object out of mapEntry.getValue() – krock Jun 15 '10 at 12:05
5

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> 
user007
  • 500
  • 1
  • 12
  • 24
2
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>
0
<logic:iterate name="FormName" property="formProperty"
                            id="list" indexId="sno">
                    <tr>
                                <td><bean:write name="list" property="value.giftproductVariable" /></td>
                                <td><bean:write name="list" property="value.giftproductVariable" /></td>

                            </tr>
                        </logic:iterate>
Ajay Takur
  • 6,079
  • 5
  • 39
  • 55