Struts2 sort标签

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/84952637

传统的页面例如购书系统,都会有根据作者的名称或者价格进行排序,在以前的解决方法中,都依赖的是底层的sql语句来解决的;

现在我们在web资源里面就可以自己解决了,减少了不少的sql底层代码和servlet代码;

下面我们就开始学习一下这个知识点把!

第一步,就是一个Action类,里面有属性有方法

package cn.com.action;

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

import org.apache.struts2.interceptor.RequestAware;

public class Person implements RequestAware{
private String name;
private Integer age;
private List<Person> list=new ArrayList<Person>();
public List<Person> getList() {
	return list;
}
public void setList(List<Person> list) {
	this.list = list;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Integer getAge() {
	return age;
}
public void setAge(Integer age) {
	this.age = age;
}
public Person(String name, Integer age) {
	super();
	this.name = name;
	this.age = age;
}
public Person(){}

public String execute(){
	list.add(new Person("EQ",14));
	list.add(new Person("QQ",11));
	list.add(new Person("CQ",15));
	list.add(new Person("SS",13));
	list.add(new Person("WW",12));
	PersonCompare com=new PersonCompare();
	requetmap.put("com", com);
	return "success";
}
private Map<String, Object> requetmap;
@Override
public void setRequest(Map<String, Object> arg0) {
	requetmap=arg0;
}
}

第二步 就是一个接口类,继承Comparator<T>接口

package cn.com.action;

import java.util.Comparator;

public class PersonCompare implements Comparator<Person>{
/*
 * author:命运的信徒
 * date:2018/12/11
 * arm:sort标签的排序
 */
	@Override
	public int compare(Person o1, Person o2) {
		// TODO Auto-generated method stub
		return o1.getAge().compareTo(o2.getAge());
	}

}

第三步 web资源的排序

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>sort标签排序</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  <body>
  <!-- comparator="排序的方式" soure="集合名称" var="属性的名称" 代表的就是Comparator<Person>里面的属性-->
  <s:sort comparator="#request.com" source="list" var="pp">
  <!--  value="#attr.person" 循环遍历的对象-->
  <s:iterator value="#attr.pp"  >
  ${name }--
  ${age }<br>
  </s:iterator>
  </s:sort>
  </body>
</html>

struts.xml

<action name="gg" class="cn.com.action.Person" method="execute">
<!-- result="名称" 当名称是error的时候,代表着对应的是<result name="error"></result>这个信息;exception="异常全类名"-->
<!-- <exception-mapping result="error" exception="java.lang.ArithmeticException">

</exception-mapping> 
<result name="error">/context.jsp</result> -->
<result name="success">/sort.jsp</result>
</action>

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/84952637