Struts2 iterator標籤
創建動作類:
首先,讓我們創建一個簡單的類叫做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" %>
<s:iterator value="employees"> <s:property value="name"/> , <s:property value="department"/>
Employees sorted by Department
<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"/>
SubSet Tag - Employees working in Recruitment department
<s:subset decider="recruitmentDecider" source="employees"> <s:iterator> <s:property value="name"/> , <s:property value="department"/>
SubSet Tag - Employees 2 and 3
<s:subset start="1" count="2" source="employees"> <s:iterator> <s:property value="name"/> , <s:property value="department"/>
讓我們通過使用一個標籤:
iterator標籤
我們使用iterator標籤要經過員工列表。我們提供「employees」屬性iterator標籤作爲源。在body迭代器標籤,我們現在有訪問Employee對象在員工列表。我們打印隨後他們部門的員工的名字。
SORT 標籤
首先,我們聲明一個bean DepartmentComparator。我們給這個bean名稱deptComparator。然後,我們使用的形式的標記,並指定作爲源和作爲比較器使用的的「deptComparator」的「僱員」列表中。然後,按照前面的例子中,我們遍歷列表和打印員工。正如可以看到的輸出,打印部門排序的員工列表
SUBSET 標籤
用來獲取的列表或陣列的一個子集,該子集標記。我們有兩種口味的子標籤。在第一個例子,我們使用recrutimentDecider 招聘部(請參閱getRecruitmentDecider()方法在Employee.java)工作的員工以獲取列表。
在第二個例子中,我們沒有使用任何決策者,而是我們所追求的元素列表中的第2和第3。子標記需要兩個參數「count」和「start」。 「start」的子集確定的起點,「count」的子集確定的長度。
配置文件
struts.xml 應該像這樣:
web.xml中,應該像這樣:
<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">
右鍵點擊項目名稱,並單擊 Export > WAR File創建一個WAR文件。然後部署此WAR在Tomcat的webapps目錄下。最後,啓動Tomcat服務器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/employee.action。這會給出以下畫面: