Struts2 <s:sort>標籤範例


Struts2的<s:sort>標籤使用 java.util.Comparator 來排序列表,這裡建立一個Web工程:struts2sorttag,來演示在多個核取方塊如何設定的預設值,整個專案的結構如下圖所示:



在本教學中,將建立6個 Person物件並新增到一個ArrayList,並使用排序標籤基於Person屬性的ArrayList進行排序。

要使用這個Struts2的<s:sort>標籤, 必須了解了java.util.Comparator 是如何工作的

1. Person 物件

建立一個有"firstName」, 「lastName」 和 「age」屬性的Person物件。

Person.java

package com.tw511.common;

public class Person{
	
	private String firstName;
	private String lastName;
	private int age;
	
	public Person(String firstName, String lastName, int age) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
	}
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

2. Comparator

建立2個Comparator類,對Person的「firstName」 和 「age」 屬性進行排序。

FirstNameComparator.java

package com.tw511.common;

import java.util.Comparator;
import com.tw511.common.Person;

public class FirstNameComparator implements Comparator<Person> {

	public int compare(Person p1, Person p2) {
		
		String firstName1 = p1.getFirstName().toUpperCase();
		String firstName2 = p2.getFirstName().toUpperCase();
		
		return firstName1.compareTo(firstName2);
	}
}

AgeComparator.java

package com.tw511.common;

import java.util.Comparator;
import com.tw511.common.Person;

public class AgeComparator implements Comparator<Person> {

	public int compare(Person p1, Person p2) {
		
		return p1.getAge() - p2.getAge();
		
	}	
}

3. 動作-Action

Action類,初始化6個 Person 物件,並把它新增到一個ArrayList。
package com.tw511.common.action;

import java.util.ArrayList;
import java.util.List;

import com.tw511.common.Person;
import com.opensymphony.xwork2.ActionSupport;
 
public class SortTagAction extends ActionSupport{
	
	private List<Person> persons = new ArrayList<Person>();
	
	public String execute() {
		
		Person p1 = new Person("C-First", "C-Last", 40);
		Person p2 = new Person("A-First", "A-Last", 20);
		Person p3 = new Person("B-First", "B-Last", 10);
		Person p4 = new Person("F-First", "F-Last", 33);
		Person p5 = new Person("E-First", "E-Last", 22);
		Person p6 = new Person("D-First", "D-Last", 11);
		
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
		persons.add(p4);
		persons.add(p5);
		persons.add(p6);
 
		return SUCCESS;
	}

	public List<Person> getPersons() {
		return persons;
	}
	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}
}

4. <s:sort>標籤範例

JSP頁面使用sort標籤與「FirstNameComparator」和「AgeComparator」對列表進行排序。

sort.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
 <html>
<head><title>struts2 sort標籤範例 - www.tw511.com</title>
</head>
 
<body>
<h1>Struts 2 Sort標籤範例</h1>

<s:bean name="com.tw511.common.FirstNameComparator" var="firstNameComparator" />
<s:bean name="com.tw511.common.AgeComparator" var="ageComparator" />

<table>
<tr>
<td>
1. Display all persons (unsort).
<ol>
<s:iterator value="persons">
     <li><s:property value="firstName" />, 
         <s:property value="lastName" />, 
         <s:property value="age" />
     </li>
</s:iterator>
</ol>
</td>

<td>
2. Display all persons (sort with firstName).
<s:sort comparator="#firstNameComparator" source="persons">
<ol>
<s:iterator>
     <li><s:property value="firstName" />, 
         <s:property value="lastName" />, 
         <s:property value="age" />
     </li>
</s:iterator>
</ol>
</s:sort>
</td>
</tr>

<tr>
<td colspan="2">
3. Display all persons (sort with age).
<s:sort comparator="#ageComparator" source="persons">
<ol>
<s:iterator>
     <li><s:property value="firstName" />, 
         <s:property value="lastName" />, 
         <s:property value="age" />
     </li>
</s:iterator>
</ol>
</s:sort>
</td>
</tr>
</table>

</body>
</html>

5. 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="default" namespace="/" extends="struts-default">
		<action name="sortTagAction" 
			class="com.tw511.common.action.SortTagAction" >
			<result name="success">pages/sort.jsp</result>
		</action>
	</package>
</struts>

4. 範例

http://localhost:8080/strut2sorttag/sortTagAction.action

在瀏覽器開啟上面的URL,顯示結果如下:

參考

  1. Struts 2 Sort 標籤文件
  2. Struts 2 Bean 標籤文件
  3. Java object sorting 範例


下載程式碼 - http://pan.baidu.com/s/1pJy6dth