`

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>&nbsp;");
		  }else{
			  out.print("<span onclick=page("+(pageNumber-1)+") style=\"cursor: hand;font-size:12px\">尾页</span>&nbsp;");
		  }
		  out.print("<input type=hidden id='curPage' name=curPage>");
		  out.print("<script type=\"text/javascript\">");
		  out.print("function page(n){");
		  out.print("document.getElementById(\"curPage\").value=n;");
		  out.print("document.getElementById(\"pageForm\").submit();");
		  out.print("}");
		  out.print("</script>");
		  }
		  
	  } catch (IOException e) {
		  e.printStackTrace();
	  }
	  return super.doStartTag();
	}
}



2、创建jstl的tld文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	version="2.0">
	<description>My Taglib For Holle Word!</description>
	<tlib-version>1.0</tlib-version>
	<short-name>page</short-name>
	<uri></uri>
	<tag>
		<name>outpage</name>
		<tag-class>com.utils.PaginationTag</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>curPage</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		<attribute>
			<name>totalPage</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		<attribute>
			<name>pageSize</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
	</tag>
</taglib>



3、测试
<%@ taglib prefix="page" uri="/WEB-INF/Pagination.tld"%>
...
<% 
	//此处模拟后台传入数据
	request.setAttribute("curPage","0");
	request.setAttribute("pageSize","5");
	request.setAttribute("totalPage","12");

%>

...

<body>
    <!-- 修改下面的index.action -->
    <form action="/index.action" id="pageForm" method="post" style="display: inline;">
		第[<font color="red">${ curPage+1}</font>/${pageCount }]页 共[${ totalPage }]条信息
				<page:outpage pageSize="${pageSize}"
					totalPage="${totalPage}" curPage="${curPage}" />
		
	</form>
  </body>

4、效果图


  • 大小: 5.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics