<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>西安小升初</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>
package com.tutorialspoint.struts2;
import java.util.Comparator;
public class DepartmentComparator implements Comparator {
private Employee e1, e2;
public int compare(Employee e1, Employee e2) {
System.out.println("输出结果并没有按部门排序");
return e1.getDepartment().compareTo(e2.getDepartment());
}
@Override
public int compare(Object arg0, Object arg1) {
System.out.println("does it come here");
// Employee e1=(Employee).arg0;
e1 = (Employee) arg0;
e2 = (Employee) arg1;
return compare(e1, e2);
}
}
原文没有16到21行,他return 0,这样,运行结果根本就没排序。
SORT TAG
First of all we declared the DepartmentComparator as a bean. We gave this bean a name deptComparator. Then we used the sort tag and specify the "employees" list as the source and the "deptComparator" as the comparator to use. Then, as per the previous example, we iterate the list and print the employees. As you can see from the output, this prints the list of employees sorted by department
SUBSET TAG
The subset tag is used to get a sub set of the list or array. We have two flavours of subset tag. In the first example, we use the recrutimentDecider to get the list of employees who work for the recruitment department (please see the getRecruitmentDecider() method in Employee.java).
In the second example, we are not using any deciders but instead we are after elements 2 and 3 in the list. The subset tag takes in two parameters "count" and "start". "start" determines the starting point of the subset and the "count" determines the length of the subset.
public String execute() {
employees = new ArrayList();
employees.add(new Employee("乔治","人力"));
employees.add(new Employee("Danielle","会计"));
employees.add(new Employee("Melissa","人力"));
employees.add(new Employee("赖斯","会计"));
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("人力");
}
};
}
原文: http://www.tutorialspoint.com/struts_2/struts_iterator_tags.htm
源代码:http://pan.baidu.com/share/link?shareid=474859&uk=3878681452