struts实现分页处理

 

网上看到,拿来学习下

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"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值