当在做项目时,很多地方需要分页,而又不想引用很繁琐复杂的分页插件,那么可以自己写一个。。

分页jsp:page_plugins.jsp

<%@ page contentType="text/html; charset=GBK"%>
<%@page import="com.opensymphony.oscache.util.StringUtil"%>
<%@page import="server.helper.PageListHelper"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.Enumeration"%>
<%
String str_iPageSize = (String)request.getAttribute(PageListHelper.SIGN_IPAGESIZE); // 每页显示条数多少
String str_iPageNo = (String)request.getAttribute(PageListHelper.SIGN_IPAGENO); // 当前页
String str_iCountAll = (String)request.getAttribute(PageListHelper.SIGN_ICOUNTALL); // 总记录数
String goto_page = (String)request.getAttribute("javax.servlet.forward.request_uri"); // 列表页面Url

int iPageNo = Integer.parseInt(StringUtil.isEmpty(str_iPageNo)?"1":str_iPageNo); // 当前页 iPageNo 
int iPageSize = Integer.parseInt(StringUtil.isEmpty(str_iPageSize)?PageListHelper.DEFAULT_PAGESIZE+"":str_iPageSize ); //页面显示条数
int iCountAll = Integer.parseInt(StringUtil.isEmpty(str_iCountAll)?"0":str_iCountAll); //总记录
int o_pageall = iCountAll/iPageSize; // 总页数
if (iCountAll%iPageSize > 0)
{
    o_pageall = o_pageall + 1;
}
//如果当前页大于总页数 当总页数为0
if(iPageNo > o_pageall)
{
    iPageNo = 0;
}
%>
<script type="text/javascript">
function showsize(){
    var iPageSize=document.pageRecordForm.iPageSize.value;
    if(iPageSize.length==0||checknumber(iPageSize)==false){
        alert("输入条数有误!");
        return;
    }
    if(iPageSize<1||iPageSize>parseInt(document.pageRecordForm.iCountAll.value)){
        alert("输入条数不在范围1-"+document.pageRecordForm.iCountAll.value+"内!");
        return;
    }
    document.pageRecordForm.iPageNo.value=1;
    document.pageRecordForm.target="_self";
    document.pageRecordForm.submit();
}
// 第一页
function hrefFirstPageOnclick(){
    document.pageRecordForm.iPageNo.value=1;
    document.pageRecordForm.target="_self";
    document.pageRecordForm.submit();
}
// 上一页
function hrefPreviousPageOnclick(){
    document.pageRecordForm.iPageNo.value=parseInt(document.pageRecordForm.iPageNo.value)-1;
    document.pageRecordForm.target="_self";
    document.pageRecordForm.submit();
}
// 下一页
function hrefNextPageOnclick(){
    document.pageRecordForm.iPageNo.value=parseInt(document.pageRecordForm.iPageNo.value)+1;
    document.pageRecordForm.target="_self";
    document.pageRecordForm.submit();
}
// 最后一页
function hrefLastPageOnclick(){
    document.pageRecordForm.iPageNo.value=document.pageRecordForm.iPageAll.value;
    document.pageRecordForm.target="_self";
    document.pageRecordForm.submit();
}
// 转到
function goto(){
    var iPageNo=document.pageRecordForm.iPageNo.value;
    if(document.pageRecordForm.iPageAll.value < 1)
    {
        alert("对不起,暂时还没有符合条件的记录,不能进行翻页.");
        return;
    }
    if(iPageNo.length==0||checknumber(iPageNo)==false){
        alert("输入页码有误!");
        return;
    }
    if(iPageNo<1||iPageNo>parseInt(document.pageRecordForm.iPageAll.value)){
        alert("输入页码不在范围1-"+document.pageRecordForm.iPageAll.value+"内!");
        return;
    }
    document.pageRecordForm.target="_self";
    document.pageRecordForm.submit();
}
function checknumber(strValue){
    if(strValue.search(/^[0-9]+[.]?[0-9]*$/)==-1) return false;
    return true;
}
</script>
<form name="pageRecordForm" id="pageRecordForm" method="post" onsubmit="return false;" action="<%=goto_page %>">
<%

//所有查询条件
for (Enumeration<String> keyNames = request.getAttributeNames(); keyNames.hasMoreElements();)
{
    String key = keyNames.nextElement();
    if (PageListHelper.notHiddenKey(key))
    {
        continue;
    }
    %>
        <input type="hidden" name="<%=key %>" value="<%=request.getAttribute(key) %>"/>
    <%
}
%>
<table border="0" cellpadding="0" cellspacing="0" width="90%" bordercolorlight="#999999" bordercolordark="#ffffff" align="center">
  <tr>
    <td colspan="20" align="center">
      <br>
      总记录数:<%=iCountAll%>
      <input type="hidden" name="iCountAll" value="<%=iCountAll%>">
      每页显示<input type="text" size="3" maxlength="3" name="iPageSize" value="<%=iPageSize%>" style="text-align:center;" onkeydown="if(event.keyCode==13) return showsize(this.form);">条记录
      <br><%=iPageNo%>页/共<%=o_pageall%><input type="hidden" name="iPageAll" value="<%=o_pageall%>">
<%
            if(iPageNo==1 || iPageNo == 0){
%>
      首页
<%
            }else{
%>
      <a href="#" name="hrefFirstPage" title="点击进入首页" onclick="hrefFirstPageOnclick();" class="black">首页</a>
<%
            }
            if(iPageNo==1 || iPageNo == 0){
%>
      上一页
<%
            }else{
%>
      <a href="#" name="hrefPreviousPage" title="点击进入上一页" onclick="hrefPreviousPageOnclick();" class="black">上一页</a>
<%
            }
            if(iPageNo==o_pageall){
%>
      下一页
<%
            }else{
%>
      <a href="#" name="hrefNextPage" title="点击进入下一页" onclick="hrefNextPageOnclick();" class="black">下一页</a>
<%
            }
            if(iPageNo==o_pageall){
%>
      尾页
<%
            }else{
%>
      <a href="#" name="hrefLastPage" title="点击进入尾页" onclick="hrefLastPageOnclick();" class="black">尾页</a>
<%
            }
%>
      跳转至<input type="text" size="3" maxlength="3" name="iPageNo" value="<%=iPageNo%>" style="text-align:center;" onkeydown="if(event.keyCode==13) return goto();"></td>
  </tr>
</table>
View Code

相关文章: