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) {
this.itemCount = itemCount;
this.itemPerPage = itemPerPage;
this.currentPage = 1;
} public Pages(int itemCount, int itemPerPage, int currentPage) {
this.itemCount = itemCount;
this.itemPerPage = itemPerPage;
this.currentPage = currentPage;
} public Pages(Collection collection, IFormPages formPages) {
this.itemCount = collection.size();
this.itemPerPage = formPages.getItemPerPage();
this.currentPage = 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;
}
}
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) {
this.itemCount = itemCount;
this.itemPerPage = itemPerPage;
this.currentPage = 1;
} public Pages(int itemCount, int itemPerPage, int currentPage) {
this.itemCount = itemCount;
this.itemPerPage = itemPerPage;
this.currentPage = currentPage;
} public Pages(Collection collection, IFormPages formPages) {
this.itemCount = collection.size();
this.itemPerPage = formPages.getItemPerPage();
this.currentPage = 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;
}
}
解决方案 »
- 写一个类倒叙输出“hello,world”
- JPA中merge方法的原理
- ajax in action 第三章 3.5 第三方的库和框架 提到的x库是什么
- struts2部署到tomcat上运行时
- 大家快加入2846761java火拼.net&ruby
- struts1.1中要验证文字长度不大于50在validation.xml文件中怎么写??(高分求解,进者有分),菜鸟真苦恼啊!
- 100分请求帮忙(关于hibernate的hibernate.cfg.xml)
- struts问题,大家研究研究
- JSP文件在websphere下怎样发布?
- 谁有JBUILDER 8 或 JBUILDER 9 ,7 也行,下载
- 我感觉hibernate设计失败的地方!
- 急!tomcat报错,到出现什么问题?
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;
}
}
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"/>