首先,讓我們建立一個簡單的類叫做Employee.java,它看起來像:
package com.yiibai.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 和contractors。我們有一個方法叫做getRecruitmentDecider,返回Decider 物件。Decider 實現返回true,如果雇員招聘部門工作,否則返回false。
接下來,讓我們建立一個DepartmentComparator比較Employee物件:
package com.yiibai.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.yiibai.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>
讓我們通過使用一個標籤:
我們使用iterator標籤要經過員工列表。我們提供“employees”屬性iterator標籤作為源。在body疊代器標籤,我們現在有存取Employee物件在員工列表。我們列印隨後他們部門的員工的名字。
首先,我們宣告一個bean DepartmentComparator。我們給這個bean名稱deptComparator。然後,我們使用的形式的標記,並指定作為源和作為比較器使用的的“deptComparator”的“雇員”列表中。然後,按照前面的例子中,我們遍歷列表和列印員工。正如可以看到的輸出,列印部門排序的員工列表
用來獲取的列表或陣列的一個子集,該子集標記。我們有兩種口味的子標籤。在第一個例子,我們使用recrutimentDecider 招聘部(請參閱getRecruitmentDecider()方法在Employee.java)工作的員工以獲取列表。
在第二個例子中,我們沒有使用任何決策者,而是我們所追求的元素列表中的第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.yiibai.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>
右鍵點選專案名稱,並單擊 Export > WAR File建立一個WAR檔案。然後部署此WAR在Tomcat的webapps目錄下。最後,啟動Tomcat伺服器和嘗試存取URL http://localhost:8080/HelloWorldStruts2/employee.action。這會給出以下畫面: