Java 在struts应用程序中迭代JSP中的hashmap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3044447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterating over hashmap in JSP in struts application
提问by Pedantic
I have a HashMap
object that I am getting on a JSP page.
我HashMap
在 JSP 页面上有一个对象。
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 上显示内容。本Gift_product
类包含两个字段:name
和price
。
JSP output should be
JSP 输出应该是
serial no. product name price
17 Doll 67
How can I achieve it?
我怎样才能实现它?
采纳答案by krock
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:
查看 struts<logic:iterate>
标签。在迭代 HashMap 时,每个条目都是一个java.util.Map.Entry
,要获取键(在本例中为序列号)和值(Gift_product 对象),请使用key
和value
属性,如下所示:
First set the HashSet as an attribute in your action class e.g. request.setAttribute("gift_hm", gift_hm);
and then in the jsp:
首先将 HashSet 设置为您的操作类中的一个属性,例如request.setAttribute("gift_hm", gift_hm);
,然后在 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>
回答by user2564182
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>
回答by user007
This one works for me (struts2):
这个对我有用(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>
回答by Ajay Takur
<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>