Struts-2-struts-iterator-tags
Struts 2-イテレータータグ
アクションクラスを作成する
まず、次のようなEmployee.javaという単純なクラスを作成します-
package com.finddevguides.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 の2つの属性があり、 employees の2つのリストがあります- permanent 従業員と請負業者です。 Decider オブジェクトを返す getRecruitmentDecider というメソッドがあります。
Decider実装は、従業員が*募集*部門で働いている場合は true を返し、それ以外の場合は false を返します。
次に、Employeeオブジェクトを比較する DepartmentComparator を作成します-
package com.finddevguides.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.finddevguides.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>
使用済みのタグを1つずつ見ていきましょう-
イテレータタグ
*iterator* タグを使用して従業員リストを調べています。 イテレータタグのソースとして「employees」プロパティを提供します。 イテレータタグの本文では、従業員リストのEmployeeオブジェクトにアクセスできるようになりました。 従業員の名前とその後に所属する部署を印刷します。
並べ替えタグ
まず、 DepartmentComparator をBeanとして宣言しました。 このBeanに deptComparator という名前を付けました。 次に、sortタグを使用し、ソースとして「従業員」リストを使用し、使用するコンパレーターとして「deptComparator」を指定します。 次に、前の例に従って、リストを繰り返して従業員を印刷します。 出力からわかるように、これは部門別にソートされた従業員のリストを印刷します。
サブセットタグ
*subset* タグは、リストまたは配列のサブセットを取得するために使用されます。 サブセットタグには2つのフレーバーがあります。 最初の例では、 *recrutimentDecider* を使用して、採用部門で働く従業員のリストを取得します(Employee.javaのgetRecruitmentDecider()メソッドを参照してください)。
2番目の例では、ディサイダーを使用していませんが、代わりにリストの要素2と3の後にあります。 サブセットタグは、2つのパラメーター「count」と「start」を取ります。 「開始」はサブセットの開始点を決定し、「カウント」はサブセットの長さを決定します。
構成ファイル
あなたの 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.finddevguides.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 にアクセスしてみます。 これにより、次の画面が生成されます-