Running Iterator Through Bean Stored in Map List

public class Iterator extends ActionSupport
{
   Map nmap = new HashMap<String, List<Person>>();
	
   public String execute()
   {
      Person objPerson    = new Person();

      objPerson.userName  = "Name1";
      objPerson.age 	    = 25;
      List<Person> myList = new ArrayList<Person>();
      myList.add(objPerson);
		
      nmap.put("Mugil", myList);
	
      objPerson.userName   = "Name2";
      objPerson.age 	     = 26;
      List<Person> myList2 = new ArrayList<Person>();
      myList2.add(objPerson);
		
      nmap.put("Mani", myList2);
		
      objPerson.userName    = "Name3";
      objPerson.age 	      = 27;
      List<Person> myList3  = new ArrayList<Person>();
      myList3.add(objPerson);
		
      nmap.put("Vinu", myList3);
		
      setNmap(nmap);
		
      return SUCCESS;
   }
	
   public Map getNmap() 
   {
     return nmap;
   }

   public void setNmap(Map nmap) 
   {
     this.nmap = nmap;
   }
}

Person Bean Code

class Person
{
    String userName;
    int age;

    public int getAge() {
      return age;
    }

    public void setAge(int age) {
      this.age = age;
    }

    public String getUserName() {
      return userName;
    }
	
    public void setUserName(String userName) {
      this.userName = userName;
    }
}

JSP Page Using Struts2 Tag

<table>
<s:iterator value="nmap">
     <s:iterator var="Person" value="value">
      <tr>
     	<td>
    	  <s:property value="key" />	
    	</td>
        <td>
      	  <s:property value="age" />
    	</td>
      </tr>
      </s:iterator>
</s:iterator>
</table>

Leave a reply