How to iterate hashmap in JSP using strut tag

11,829

Solution 1

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>

Solution 2

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.

Share:
11,829
Amit Kumar Gupta
Author by

Amit Kumar Gupta

I am Research Enthusiast working as a full time opensource developer. I used to develop general and generic applications which can give better performance with fewer resources. I am a greedy programmer who likes creative things.

Updated on June 04, 2022

Comments

  • Amit Kumar Gupta
    Amit Kumar Gupta almost 2 years

    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?