Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7413

How to iterate hashmap in JSP using strut tag

I can print value of a key of HashMap as below

<%
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" />

How can i print key and its value iteratively?

Upvotes: 2

Views: 12020

Answers (3)

user007
user007

Reputation: 500

This one works for me (struts2):

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

there's no need to use bean:write.

Upvotes: 1

Hemant Metalia
Hemant Metalia

Reputation: 30698

you can do this using logic:iterate

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

</logic:iterate>

refer http://www.techfaq360.com/tutorial/logiciterate.jsp

or you can also use

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

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

also go through http://struts.apache.org/2.0.11/docs/iterator.html

Upvotes: 0

Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7413

Well!! I dint get this answer anywhere after googling. But somehow, hitting the ground, I done it without using EL or scriptlets.

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

Upvotes: 4

Related Questions