一直都有人问Structs下分页怎么做,本人积累了一些经验,以下是我的实现。
我的实现相对还是比较灵活的,如果大家有什么更好的想法欢迎赐教。
ps: 里面有些类和方法没有写出来,如 CollectionUtil.copy(),其实就是集合的复制,一看就明白了,可以自己实现。
1.IFormPages接口,每个需要分页的form都继承这个接口:
package com.fellow.pub.servlet;
public interface IFormPages {
int getCurrentPage();
void setCurrentPage(int currentPage);
int getItemPerPage();
void setItemPerPage(int itemPerPage);
}
2.分页计算类Pages:
package com.fellow.pub.util;
import java.util.Vector;
import java.util.Collection;
import com.fellow.pub.servlet.IFormPages;
public class Pages {
protected int itemCount;
protected int itemPerPage;
protected int currentPage;
public Pages() {
this.itemCount = 0;
this.itemPerPage = 0;
this.currentPage = 1;
}
public Pages(int itemCount, int itemPerPage) throws Exception{
this(itemCount, itemPerPage, 1);
}
public Pages(int itemCount, int itemPerPage, int currentPage) throws Exception{
this.itemCount = (itemCount <= 0)?0:itemCount;
this.itemPerPage = (itemPerPage <= 0)?20:itemPerPage;
int curPage = (currentPage <= 0)?1:currentPage;
int pageCount = (itemCount % itemPerPage == 0)?(itemCount / itemPerPage):(itemCount / itemPerPage + 1);
if(curPage > pageCount)curPage = pageCount;
this.currentPage = curPage;
}
public Pages(Collection collection, IFormPages formPages) throws Exception{
this(collection.size(), formPages.getItemPerPage(), formPages.getCurrentPage());
}
public int getItemCount() {
return this.itemCount;
}
public void setItemCount(int itemCount) {
this.itemCount = itemCount;
}
public int getItemPerPage() {
return this.itemPerPage;
}
public void setItemPerPage(int itemPerPage) {
this.itemPerPage = itemPerPage;
}
public int getCurrentPage() {
return this.currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPrePage(){
return this.currentPage - 1;
}
public int getNextPage(){
return this.currentPage + 1;
}
protected void AssertErrors() throws Exception{
if(itemPerPage <= 0 ||
currentPage <= 0 ||
itemCount < itemPerPage * (currentPage - 1)){
throw new Exception("Pages object calculated error");
}
}
public int getPageCount() throws Exception{
if(itemCount <= 0) return 0;
AssertErrors();
if(itemCount % itemPerPage == 0){
return itemCount / itemPerPage;
}
else{
return itemCount / itemPerPage + 1;
}
}
public int getPageItemCount() throws Exception{
if(itemCount <= 0) return 0;
int pageCount = getPageCount();
if(pageCount == currentPage){
return itemCount -itemPerPage * (currentPage - 1);
}
else{
return itemPerPage;
}
}
public int getPageItemCount(int currentPage) throws Exception{
if(itemCount <= 0) return 0;
int pageCount = getPageCount();
if(pageCount == currentPage){
return itemCount -itemPerPage * (currentPage - 1);
}
else{
return itemPerPage;
}
}
public boolean isFirstPage() throws Exception{
return (currentPage == 1);
}
public boolean isLastPage() throws Exception{
int pageCount = getPageCount();
return (pageCount == currentPage);
}
public Collection getIndexList() throws Exception{
int pageCount = getPageCount();
Vector result = new Vector(pageCount);
for(int i = 0; i < pageCount; i++){
result.add(new Integer(i + 1));
}
return result;
}
}
3.分页集合类CollectionPages,继承Pages:
package com.fellow.pub.util;
import java.util.*;
import com.fellow.pub.servlet.IFormPages;
public class CollectionPages extends Pages {
protected Collection collection = null;
protected Collection collectionPage = null;
public CollectionPages(Collection collection,
IFormPages formPages) throws Exception {
super(collection, formPages);
this.collection = collection;
buildCollectionPage();
}
public Collection getCollection(){
return this.collection;
}
public void setCollection(Collection collection){
this.collection = collection;
}
public Collection getCollectionPage(){
return this.collectionPage;
}
public Collection buildCollectionPage() throws Exception{
if(collection == null){
throw new Exception("collection object is null");
}
collectionPage = new Vector(itemPerPage);
CollectionUtil.copy(collection, collectionPage,
(currentPage - 1) * itemPerPage,
(currentPage) * itemPerPage);
return collectionPage;
}
}
4.Action里面初始化:
CollectionPages collectionPages = new CollectionPages(collection, formPages);
其中collection就是需要分页的集合
5.保存到request
setAttribute(name, collectionPages);
6.在页面上使用collectionPages对象,collectionPages对象有两个子对象:
i.collectionPage,就是分页以后的集合
<bean:define id="collectionList" name="collectionPages" property="collectionPage" scope="request" />
ii.indexList就是页码(由于我不知道怎么用简单的方法处理页码,就只好弄这样一个对象来iterate了)
<bean:define id="indexList" name="collectionPages" property="indexList" scope="request"/>