Home » struts 2 iterator tag example

struts 2 iterator tag example

by Online Tutorials Library

Struts 2 Iterator Tag example

1) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.

index.jsp

<a href="fetch">findPartner</a> 

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

Find.java

package mypack; import java.util.ArrayList; public class Find { private ArrayList list=new ArrayList(); public ArrayList getList() { return list; } public void setList(ArrayList list) { this.list = list; }  public String execute(){ User u1=new User(); u1.setUserName("Amit"); u1.setUserPass("kumar"); u1.setEmail("[email protected]");  User u2=new User(); u2.setUserName("Vijay"); u2.setUserPass("kumar"); u2.setEmail("[email protected]");  list.add(u1); list.add(u2);  return "success"; } } 

3) Create the model

User.java

package mypack; public class User { private String userName,userPass,email; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPass() { return userPass; } public void setUserPass(String userPass) { this.userPass = userPass; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } 

4) Create struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"  "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="arr" extends="struts-default"> <action name="fetch" class="mypack.Find" method="execute"> <result name="success">welcome.jsp</result> </action> </package> </struts>     

5) Create view component

It is the simple jsp file displaying the information of the user.

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s" %> Data is:<br/> <s:iterator value="list"> <fieldset> <table width="40%"> <tr><td><s:property value="userName"/></td></tr> <tr><td><s:property value="userPass"/></td></tr> <tr><td><s:property value="email"/></td></tr> </table> </fieldset> </s:iterator> 

You may also like