java 后台代码实现分页 demo 实力 自己写的 标签分页
package com.cszoc.sockstore.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
/**
* 分页JavaBean
*
*
*/
public class PageBean {
/* 当前页 */
private int curPage=1;
/* 总记录数 */
private int totalRecord = 0;
/* 每页记录数 */
private int pageRecord = 10;
/* 是否分页,默认分页 */
private boolean paginate = true;
/* 请求的URL */
private String url;
/* 请求中的参数 */
private Map parameters = new HashMap();
public PageBean() {
super();
}
public PageBean(HttpServletRequest request) {
super();
this.setRequest(request);
}
/**
* 使用HttpServletRequest对象对分页Bean进行初始化
*
*/
@SuppressWarnings("unchecked")
public void setRequest(HttpServletRequest request) {
if(request.getAttribute("com.accp.book.util.PageBean")!=null)
{
PageBean p=(PageBean) request.getAttribute("com.accp.book.util.PageBean");
copy(p);
}
else
{
this.setCurPage(request.getParameter("curPage"));// 从请求中获取当前页
this.setPageRecord(request.getParameter("pageRecord"));// 从请求中获取页大小
this.setPaginate(request.getParameter("paginate"));// 获取用来判断是否分页的参数
//System.out.println("001");
//System.out.println(request.getContextPath());
//System.out.println(request.getServletPath());
//System.out.println(this);
//System.out.println(request);
//System.out.println("001");
this.setUrl(request.getContextPath() + request.getServletPath());// 获取表示请求资源的URL
this.setParameters(new HashMap(request
.getParameterMap()));// 获取请求中的所有参数
}
request.setAttribute("com.accp.book.util.PageBean", this);// 将分页Bean保存request作用域当中
}
/**
* true表示进行分页
*
* @return
*/
public boolean isPaginate() {
return paginate;
}
/**
* 设置是否分页true分页,false不分页
*
* @param paginate
*/
public void setPaginate(boolean paginate) {
this.paginate = paginate;
}
/**
* 设置是否分页true分页,false不分页
*
* @param paginate
*/
public void setPaginate(String paginate) {
if (null != paginate && paginate.trim().equalsIgnoreCase("false")) {
this.setPaginate(false);
} else {
this.setPaginate(true);
}
}
public Map getParameters() {
return parameters;
}
public void setParameters(Map parameters) {
this.parameters = parameters;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
if (curPage > 1) {
this.curPage = curPage;
}
}
public void setCurPage(String curPage) {
if (null == curPage || curPage.trim().equals("")) {
return;
}
int acurPage = Integer.parseInt(curPage);
this.setCurPage(acurPage);
}
public int getPageRecord() {
return pageRecord;
}
public void setPageRecord(int pageRecord) {
this.pageRecord = pageRecord;
}
public void setPageRecord(String pageRecord) {
if (null != pageRecord && !pageRecord.trim().equals("")) {
this.pageRecord = Integer.parseInt(pageRecord);
}
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
/**
* 返回下一页页码
*
* @return
*/
public int getNextPageNumber() {
int nextPageNumber = this.curPage + 1;
if (nextPageNumber > this.getMaxPageNumber()) {
nextPageNumber = this.getMaxPageNumber();
}
return nextPageNumber;
}
/**
* 返回上一页页码
*
* @return
*/
public int getPreviousPageNumber() {
int previousPageNumber = this.curPage - 1;
if (previousPageNumber < 1) {
previousPageNumber = 1;
}
return previousPageNumber;
}
/**
* 返回最大页码编号
*
* @return
*/
public int getMaxPageNumber() {
int maxPageNumber = 1;
if (this.totalRecord > this.pageRecord) {
if (0 == this.totalRecord % this.pageRecord) {
maxPageNumber = this.totalRecord / this.pageRecord;
} else {
maxPageNumber = this.totalRecord / this.pageRecord + 1;
}
}
return maxPageNumber;
}
/**
* 获取起始记录下标
*
* @return
*/
public int getStartRecordIndex() {
return this.pageRecord * (this.curPage - 1);
}
public String toString() {
StringBuffer bf = new StringBuffer();
bf.append("curPage=" + curPage);
bf.append(", totalRecord=" + totalRecord);
bf.append(", pageRecord=" + pageRecord);
bf.append(", hashcode=" + hashCode());
return bf.toString();
}
private void copy(PageBean p)
{
this.curPage=p.curPage;
this.pageRecord=p.pageRecord;
this.paginate=p.paginate;
this.parameters=p.parameters;
this.totalRecord=p.totalRecord;
this.url=p.url;
}
}
html 标签前台分页
--------------------------------------------------------------------------
package com.cszoc.sockstore.util;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 通用分页标签
*
*/
public class PageBeanTag extends BodyTagSupport {
private static final long serialVersionUID = 8450962461296575662L;
public PageBeanTag() {
super();
}
@Override
public int doStartTag() throws JspException {
PageBean pageBean = (PageBean) pageContext.getRequest().getAttribute(
"com.accp.book.util.PageBean");
if (null != pageBean && pageBean.isPaginate()) {
if(null!=pageContext.getRequest().getAttribute("reseturl"))
{
pageBean=(PageBean) pageContext.getRequest().getAttribute("reseturl");
}
try {
//System.out.println("002");
//System.out.println(pageBean.getUrl());
//System.out.println(pageBean);
//System.out.println("002");
pageContext.getOut().write(toHTML(pageBean));
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
pageContext.getRequest().removeAttribute("com.accp.book.util.PageBean");
return SKIP_BODY;
}
/* 将分页JavaBean转换成HTML字符串 */
private String toHTML(PageBean pageBean) {
// 保存请求的参数到一个表单对象中,以供下次查询使用
StringBuffer bf = new StringBuffer();
bf.append("
");bf.append("");// 当前页是目前是空,但在点击分页按钮时会进行设置
Map parameters = pageBean.getParameters();// 请求中的所有参数
String paramName = null;
String[] paramValues = null;
Iterator it = parameters.keySet().iterator();
while (it.hasNext()) {
paramName = (String) it.next();// 参数名
if ("curPage".equals(paramName)) {
continue;// 把当前页参数忽略掉
}
paramValues = parameters.get(paramName);// 参数值
for (int i = 0; null != paramValues && i < paramValues.length; i++) {
bf.append("");
}
}
bf.append("
");// 显示分页按钮
//bf.append("每页 " + pageBean.getPageRecord() + " 行");
//bf.append(" 共 " + pageBean.getTotalRecord() + " 行");
//bf.append(" 第 " + pageBean.getCurPage() + " 页");
//bf.append(" 共 " + pageBean.getMaxPageNumber() + " 页");
bf.append("
- ");
bf.append("
第"+pageBean.getCurPage()+"页/共"+pageBean.getMaxPageNumber()+"页 首页"+" ");if(pageBean.getCurPage()!=1){
bf.append(" "+"
上一页"+" ");}
if(pageBean.getCurPage()<=3)
{
if(pageBean.getMaxPageNumber()<=3)
{
for(int i=1;i<=pageBean.getMaxPageNumber();i++)
{
if(pageBean.getCurPage()==i)
{
bf.append(" "+"
"+i+""+" ");i+=1;
}
if(pageBean.getMaxPageNumber()>=i){
bf.append(" "+"
"+i+""+" ");}
}
}
else
{
for(int i=1;i<=3;i++){
if(pageBean.getCurPage()==i){
bf.append("
"+i+"");i+=1;
}
if(pageBean.getMaxPageNumber()>=i){
bf.append(" "+"
"+i+""+" ");}
}
}
}
if(pageBean.getCurPage()3&&pageBean.getMaxPageNumber()>=3)
{
for(int i=pageBean.getCurPage()-2;i<=pageBean.getCurPage()+2;i++)
{
if(pageBean.getCurPage()==i)
{
bf.append("
"+i+"");i+=1;
}
bf.append(" "+"
"+i+""+" ");}
}
if(pageBean.getCurPage()>=pageBean.getMaxPageNumber()-3&&pageBean.getMaxPageNumber()>=3&&pageBean.getCurPage()>3){
for(int i=pageBean.getMaxPageNumber()-3;i<=pageBean.getMaxPageNumber();i++){
if(pageBean.getCurPage()==i){
bf.append("
"+i+"");i+=1;
}
if(pageBean.getMaxPageNumber()>=i){
bf.append(" "+"
"+" "+i+" "+""+" ");}
}
}
if(pageBean.getCurPage()!=pageBean.getMaxPageNumber()){
bf.append(" "+"
下一页"+" ");}
bf.append(" "+"
末页");//bf.append(" "+"
跳转 ");bf.append(" "+"
到页