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> ");
//上一页
// out.print("<span class=f7_1 style=\"cursor: hand;\">< </span>");
if((Integer.parseInt(curPage)-1<0)){
out.print("<span style=\"cursor: hand;font-size:12px\">上一页</span> ");
}else{
out.print("<span style=\"cursor: hand;;font-size:12px\"" +
"onclick=page("+(Integer.parseInt(curPage) - 1)+")>上一页</span>  ");
}


// // 显示给用户操作的页面开始端
// 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>  ");
}else{
out.print("<span style=\"cursor: hand;background:bottom;color:black\" onclick=page("+(i-1)+")><font size=\"2\">"+i+"</font></span>  ");
}
}
//下一页
if(Integer.parseInt(curPage)>=pageNumber-1){
out.print("<span style=\"font-size:12px\">下一页</span><span class=f7_1>" +
"</span><span></span><span></span> ");
}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> ");
}
//尾页
if(Integer.parseInt(curPage)>=pageNumber-1){
out.print("<span style=\"font-size:12px\">尾页</span> ");
}else{
out.print("<span onclick=page("+(pageNumber-1)+") style=\"cursor: hand;font-size:12px\">尾页</span> ");
}
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、效果图

[img]http://dl.iteye.com/upload/attachment/0065/1043/f1914ba4-59be-3a4a-a2a7-0bab12e75273.jpg[/img]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值