Struts 2 - Iterator 标签
创建操作类
首先让我们创建一个名为 Employee.java 的简单类,它看起来像 −
package com.tutorialspoint.struts2; import java.util.ArrayList; import java.util.List; import org.apache.struts2.util.SubsetIteratorFilter.Decider; public class Employee { private String name; private String department; public Employee(){} public Employee(String name,String department) { this.name = name; this.department = department; } private List employees; private List contractors; public String execute() { employees = new ArrayList(); employees.add(new Employee("George","Recruitment")); employees.add(new Employee("Danielle","Accounts")); employees.add(new Employee("Melissa","Recruitment")); employees.add(new Employee("Rose","Accounts")); contractors = new ArrayList(); contractors.add(new Employee("Mindy","Database")); contractors.add(new Employee("Vanessa","Network")); return "success"; } public Decider getRecruitmentDecider() { return new Decider() { public boolean decide(Object element) throws Exception { Employee employee = (Employee)element; return employee.getDepartment().equals("Recruitment"); } }; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public List getEmployees() { return employees; } public void setEmployees(List employees) { this.employees = employees; } public List getContractors() { return contractors; } public void setContractors(List contractors) { this.contractors = contractors; } }
Employee 类有两个属性 - name 和 department,我们还有两个 employees 列表 - 永久 员工和合同工。我们有一个名为 getRecruitmentDecider 的方法,它返回一个 Decider 对象。
如果员工在 recruitment 部门工作,则 Decider 实现返回 true,否则返回 false。
接下来,让我们创建一个 DepartmentComparator 来比较 Employee 对象−
package com.tutorialspoint.struts2; import java.util.Comparator; public class DepartmentComparator implements Comparator { public int compare(Employee e1, Employee e2) { return e1.getDepartment().compareTo(e2.getDepartment()); } @Override public int compare(Object arg0, Object arg1) { return 0; } }
如上例所示,部门比较器按字母顺序根据部门比较员工。
创建视图
创建一个名为 employee.jsp 的文件,其中包含以下内容 −
<%@ page contentType = "text/html; charset = UTF-8" %> <%@ taglib prefix = "s" uri = "/struts-tags" %> <html> <head> <title>Employees</title> </head> <body> <b>Example of Iterator Tag</b><br/> <s:iterator value = "employees"> <s:property value = "name"/> , <s:property value = "department"/><br/> </s:iterator> <br/><br/> <b>Employees sorted by Department</b> <br/> <s:bean name = "com.tutorialspoint.struts2.DepartmentComparator" var = "deptComparator" /> <s:sort comparator = "deptComparator" source = "employees"> <s:iterator> <s:property value = "name"/> , <s:property value = "department"/><br/> </s:iterator> </s:sort> <br/><br/> <b>SubSet Tag - Employees working in Recruitment department </b><br/> <s:subset decider="recruitmentDecider" source = "employees"> <s:iterator> <s:property value = "name"/> , <s:property value = "department"/><br/> </s:iterator> </s:subset> <br/><br/> <b>SubSet Tag - Employees 2 and 3 </b><br/> <s:subset start="1" count = "2" source = "employees"> <s:iterator> <s:property value = "name"/> , <s:property value = "department"/><br/> </s:iterator> </s:subset> </body> </html>
让我们逐一查看使用的标签 −
迭代器标签
我们使用 迭代器 标签查看员工列表。我们将"员工"属性作为迭代器标签的源。在迭代器标签的主体中,我们现在可以访问员工列表中的员工对象。我们打印员工的姓名,然后是他们的部门。
排序标签
首先,我们将 DepartmentComparator 声明为 bean。我们给这个 bean 命名为 deptComparator。然后我们使用排序标签并指定"员工"列表作为源,将"deptComparator"指定为要使用的比较器。然后,按照前面的示例,我们迭代列表并打印员工。从输出中可以看出,这将打印按部门排序的员工列表。
子集标签
子集标签用于获取列表或数组的子集。我们有两种类型的子集标签。在第一个示例中,我们使用recrutimentDecider来获取在招聘部门工作的员工列表(请参阅Employee.java中的getRecruitmentDecider()方法)。
在第二个示例中,我们没有使用任何决策者,而是使用列表中的元素2和3。子集标签包含两个参数"count"和"start"。 "start"确定子集的起点,"count"确定子集的长度。
配置文件
您的struts.xml应该看起来像 −
<?xml version = "1.0" Encoding = "UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name = "struts.devMode" value = "true" /> <package name = "helloworld" extends = "struts-default"> <action name = "employee" class = "com.tutorialspoint.struts2.Employee" method = "execute"> <result name = "success">/employee.jsp</result> </action> </package> </struts>
文件 web.xml 应类似于 −
<?xml version = "1.0" Encoding = "UTF-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
右键单击项目名称,然后单击导出 > WAR 文件以创建 War 文件。然后将此 WAR 部署到 Tomcat 的 webapps 目录中。最后,启动 Tomcat 服务器并尝试访问 URL http://localhost:8080/HelloWorldStruts2/employee.action。这将产生以下屏幕 −
