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.gitbook.netmon; 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.gitbook.netmon; import java.util.Comparator; import com.gitbook.netmon.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.gitbook.netmon; import java.util.Comparator; import com.gitbook.netmon.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.gitbook.netmon.action; import java.util.ArrayList; import java.util.List; import com.gitbook.netmon.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.gitbook.net</title> </head> <body> <h1>Struts 2 Sort標簽示例</h1> <s:bean name="com.gitbook.netmon.FirstNameComparator" var="firstNameComparator" /> <s:bean name="com.gitbook.netmon.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.gitbook.netmon.action.SortTagAction" > <result name="success">pages/sort.jsp</result> </action> </package> </struts>
4. 示例
http://localhost:8080/strut2sorttag/sortTagAction.action
在瀏覽器打開上麵的URL,顯示結果如下:
參考