Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2 <s:sort>标签示例
Struts2 <s:sort>标签示例
作者:--    发布时间:2019-11-20 10:16:52

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

import java.util.comparator;
import com.h3.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.h3.common;

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

import java.util.arraylist;
import java.util.list;

import com.h3.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.h3.com</title>
</head>
 
<body>
<h1>struts 2 sort标签示例</h1>

<s:bean name="com.h3.common.firstnamecomparator" var="firstnamecomparator" />
<s:bean name="com.h3.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.h3.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
所有评论

loading......

滚动到顶部 滚动到底部
合作联系(微信):
www.helper3.com
关于本站:
宗旨
网站备案号:
粤ICP备17073857号-1

声明:
本站部分内容来自网络,如您发现本站内容侵害到您的利益,请联系本站管理员处理。