我有一个HashMapJSP页面上的对象.
HashMap<Integer,Gift_product> gift_hm = new HashMap<Integer,Gift_product>();
gift_hm.put(17,new Gift_product("doll",67));
Run Code Online (Sandbox Code Playgroud)
现在我需要迭代它并在JSP上显示内容.本Gift_product类包含两个字段:name和price.
JSP输出应该是
serial no. product name price
17 Doll 67
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现它?
kro*_*ock 12
查看struts <logic:iterate>标签.当迭代HashMap时,每个条目都是a java.util.Map.Entry,以获取密钥(在此示例中为序列号)和值(Gift_product对象)out使用key和这样的value属性:
首先将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>
Run Code Online (Sandbox Code Playgroud)
这对我有用(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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25298 次 |
| 最近记录: |