2011-12-28 70 views
2

我可以打印HashMap中的一個關鍵的值,如下如何HashMap的迭代中使用JSP標籤支桿

<% 
HashMap<String,String> students = new HashMap<String,String>(); 
       students.put("1","Amit"); 
       students.put("2","Amit"); 
       students.put("3","Anil"); 
       students.put("4","Amit"); 
session.setAttribute("students", students); 
%> 
<bean:write name="students" property="1" /> 
<bean:write name="students" property="3" /> 

如何打印鍵,並將其值反覆?

回答

4

好吧! Google搜索結果後,我可以隨時隨地獲得答案。但不知怎麼的,我沒有使用EL或腳本來做到這一點。

<logic:iterate name="students" id="nameObj" scope="session"> 
     <bean:write name="nameObj" property="key"/> 
     <bean:write name="nameObj" property="value"/> 
</logic:iterate> 
0

,你可以做到這一點使用邏輯:迭代

<logic:iterate id="id" name="name"> 

</logic:iterate> 

參考http://www.techfaq360.com/tutorial/logiciterate.jsp

,或者您也可以使用

<s:property value="%{name}" /> 

參考http://www.roseindia.net/struts/struts2/struts2controltags/property-tag.shtml

也經過http://struts.apache.org/2.0.11/docs/iterator.html

+0

這些是Struts的兩個不同版本。 ''標籤的語法有點可疑;它不應該是*括號記號,*或*點記法,但不是兩個? – 2011-12-28 14:03:02

1

這一個對我的作品(Struts2的):

<s:iterator value="students" var="studentElement"> 
    <s:property value="#studentElement.key"/> 
    <s:property value="#studentElement.value"/> 
</s:iterator> 

沒有必要使用豆:寫。

相關問題