struts实现分页处理
2009-08-18 01:25
316 查看
网上看到,拿来学习下
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"/>
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"/>
相关文章推荐
- struts实现分页处理
- struts实现分页处理
- 权限管理框架实现(1)--Struts切面处理
- 在struts1中使用DispatchAction对应多个ActionForm,实现写一个Action实现增删改查操作---一个Action处理多个操作的能力
- struts+spring+hibernate用jquery实现数据分页异步加载,页面不刷新
- 在struts中分页的一种实现
- jquery+struts实现前台分页
- Struts中如何实现查询结果分页显示
- struts开发实践—分页的实现
- struts 实现分页(之二)
- AJAX+struts实现无刷新分页
- struts标签实现博客园的分页
- struts中的logic:present标签 用于实现分页功能(转)
- struts分页的一种实现
- android阅读器里的 txt 文本处理分页功能的实现:
- Struts标签实现分页(当前页码在中间显示)
- Struts+Hibernate开发实践 分页的实现
- easyui与ssh(struts1)的整合实现增删改查、分页
- 用json-lib如何来处理数据,在前台实现分页
- struts2之使用JSON插件实现Ajax(在struts.xml中处理要序列化的属性)