当前位置: 首页 > 教程 > Struts2 >

Struts2 <s:sort>标签示例

精华
小牛编辑
149浏览
2023-03-14

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.yiibai.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.yiibai.common;

import java.util.Comparator;
import com.yiibai.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.yiibai.common;

import java.util.Comparator;
import com.yiibai.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.yiibai.common.action;

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

import com.yiibai.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.yiibai.com</title>
</head>
 
<body>
<h1>Struts 2 Sort标签示例</h1>

<s:bean name="com.yiibai.common.FirstNameComparator" var="firstNameComparator" />
<s:bean name="com.yiibai.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.yiibai.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