日期:2014-05-16  浏览次数:20353 次

jstl 实现分页
1、创建分页工具类(jstl标签的实现类)
package com.utils;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/***
 * 分页控件
 * @author hugo
 *
 */
public class PaginationTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	
	/**
	  * 当前页
	  */
	private String curPage;
	/**
	  * 总tiao数
	  */
	private String totalPage;
	/**
	  * 页大小(一页显示的大小)
	  */
	private String pageSize;

	public void setCurPage(String curPage) {
	  this.curPage = curPage;
	}

	public void setPageSize(String pageSize) {
	  this.pageSize = pageSize;
	}
	public void setTotalPage(String totalPage) {
	  this.totalPage = totalPage;
	}

	public int doStartTag() throws JspException {
	  JspWriter out = pageContext.getOut();
	  // 得到分页后的页数,(总页数/页大小)+1
	  if (pageSize == null || pageSize == "") {
		  pageSize = "1";
	  }
	  int pageNumber=0;//共有页数
	  if (Integer.parseInt(totalPage)%Integer.parseInt(pageSize) == 0 ){ 
		  pageNumber = Integer.parseInt(totalPage)/Integer.parseInt(pageSize);
	  } else{
		  pageNumber = Integer.parseInt(totalPage)/Integer.parseInt(pageSize) + 1; 
	  }
	  if (Integer.parseInt(curPage) < 0) {
		  curPage = "1";
	  }
	  try {
		  if(pageNumber>1){
		  out.print("<span onclick=page(0) style=\"cursor: hand; font-size:12px\">首页</span>&nbsp;");
		  //上一页
//		  out.print("<span class=f7_1 style=\"cursor: hand;\">&lt; </span>");
		  if((Integer.parseInt(curPage)-1<0)){
			  out.print("<span style=\"cursor: hand;font-size:12px\">上一页</span>&nbsp;");
		  }else{
			  out.print("<span style=\"cursor: hand;;font-size:12px\"" +
				  		"onclick=page("+(Integer.parseInt(curPage) - 1)+")>上一页</span>&nbsp;&nbsp;");
		  }
		 
		  
//		// 显示给用户操作的页面开始端
//		  int start = Integer.parseInt(curPage) - 4;
//		  // 显示给用户操作的页面结束端
//		  int end = Integer.parseInt(curPage) + 4;
//		  // 特殊情况处理(开始端小于0)
//		  if ((Integer.parseInt(curPage) - 4) <= 1) {
//			  start = 1;
//			  	if(pageNumber>10){
//			  		 end=10-pageNumber;
//			  	}else{
//			  		 end=pageNumber;
//			  	}
//		  }
//		  // 特殊情况处理(结束端大于总页数)
//		  if ((Integer.parseInt(curPage) + 4) > Integer.parseInt(totalPage)) {
//			  end = pageNumber;
//			  start=Integer.parseInt(totalPage)-10;
//			  if(start<0){
//				  start=1;
//			  }
//		  }
		  int start=(Integer.parseInt(curPage))-4;
		  if(start<=0){
			  start = 0;
		  }
		  int end=7;
		  if(Integer.parseInt(curPage)-start==4){
			  start=start+1;
			  end=end+start;
			  if(end>=pageNumber){
				  end=pageNumber;
			  }
		  }
		  if(end>=pageNumber){
			  end=pageNumber;
		  }
		  for (int i = start+1; i <=end; i++) {
			    if(i-1== Integer.parseInt(curPage)){
			    	out.print("<span style=\"cursor: hand;border-style: solid;border-color: #ccc;background:bottom;color:white;background-color: #ccc;border-width: 1px;border-bottom-style: none;\" onclick=page("+(i-1)+")><font size=\"2\">"+i+"</font></span>&nbsp;&nbsp;");
			    }else{
			    	out.print("<span style=\"cursor: hand;background:bottom;color:black\" onclick=page("+(i-1)+")><font size=\"2\">"+i+"</font></span>&nbsp;&nbsp;");
			    }
		  }
		  //下一页
		  if(Integer.parseInt(curPage)>=pageNumber-1){
			  out.print("<span style=\"font-size:12px\">下一页</span><span class=f7_1>" +
				  				"</span><span></span><span></span>&nbsp;");
		  }else{
			  out.print("<span style=\"cursor: hand;font-size:12px\" " +
				  		"onclick=page("+(Integer.parseInt(curPage)+1)+")>下一页</span><span class=f7_1>" +
				  				"</span><span></span><span></span>&nbsp;");
		  }
		  //尾页
		  if(Integer.parseInt(curPage)>=pageNumber-1){
			  out.print("<span style=\"font-size:12px\">尾页</span>&