Struts 2迭代器标签示例

1)创建index.jsp作为输入
该jsp页面使用struts UI标记创建表单。它从用户那里接收名称, 密码和电子邮件ID。
index.jsp

< a href="http://www.srcmini.com/fetch"> findPartner< /a>

2)创建动作类
该操作类继承了ActionSupport类并覆盖了execute方法。
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("amit@gmail.com"); User u2=new User(); u2.setUserName("Vijay"); u2.setUserPass("kumar"); u2.setEmail("vijay@gmail.com"); list.add(u1); list.add(u2); return "success"; } }

3)创建模型
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)创建struts.xml
该xml文件通过名称输入和拦截器jsonValidatorWorkflowStack定义了一个额外的结果。
【Struts 2迭代器标签示例】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)创建视图组件
它是显示用户信息的简单jsp文件。
welcome.jsp
< %@ taglib uri="/struts-tags" prefix="s" %> Data is:< br/> < s:iterator value="http://www.srcmini.com/list"> < fieldset> < table width="40%"> < tr> < td> < s:property value="http://www.srcmini.com/userName"/> < /td> < /tr> < tr> < td> < s:property value="http://www.srcmini.com/userPass"/> < /td> < /tr> < tr> < td> < s:property value="http://www.srcmini.com/email"/> < /td> < /tr> < /table> < /fieldset> < /s:iterator>

下载此示例

    推荐阅读