在struts应用程序中迭代JSP中的hashmap [英] Iterating over hashmap in JSP in struts application
问题描述
我在JSP页面上有一个 HashMap
对象。
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));
现在我需要迭代这个并在JSP上显示内容。
Gift_product
类包含两个字段: name
和 price
。
Now I need to iterate this and display content on JSP.
The Gift_product
class contains two fields: name
and price
.
JSP输出应该是
serial no. product name price
17 Doll 67
我如何实现它?
推荐答案
查看struts < logic:iterate>
标记。迭代HashMap时,每个条目都是 java.util.Map.Entry
,以获取密钥(在此示例中为序列号)和值(Gift_product对象) out使用键
和值
这样的属性:
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:
首先将HashSet设置为动作类中的属性,例如 request.setAttribute(gift_hm,gift_hm);
然后在jsp中:
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>
这篇关于在struts应用程序中迭代JSP中的hashmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!