一个JSP分页组件

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<%--
    /*
        所有要接收的值为 currentPage,pageSize,allCount,URL
    */
    每次使用该组件时,修改page属性的值即可使用
            <jsp:include flush="true" page="/emp/split_page.jsp">
                <jsp:param name="currentPage" value="<%=currentPage %>"/>
                <jsp:param name="pageSize" value="<%=pageSize %>"/>
                <jsp:param name="allCount" value="<%=allCount %>"/>
                <jsp:param name="url" value="list.jsp"/>
                <jsp:param name="keyword" value="<%=keyword %>"/>
                <jsp:param name="searchFlag" value="FALSE"/>
            </jsp:include>
            
            
        <jsp:include flush="true" page="/split_page.jsp">
            <jsp:param name="currentPage" value="${empForm.cp}" />
            <jsp:param name="pageSize" value="${empForm.ps}" />
            <jsp:param name="allCount" value="${allCount}" />
            <jsp:param name="url"
                value="${pageContext.request.contextPath}/emp.do?status=list" />
            <jsp:param name="keyword" value="${empForm.kw}" />
            <jsp:param name="searchFlag" value="TRUE" />
        </jsp:include>
        <input type="button" value="返回" οnclick="history.back();">

 --%>

<%
int currentPage = 1;
int pageSize = 5 ;
int allCount = 0 ;
String url = request.getParameter("url");
String keyword = request.getParameter("keyword");
String searchFlag = request.getParameter("searchFlag");
try {
    currentPage = Integer.parseInt(request.getParameter("currentPage"));
    pageSize = Integer.parseInt(request.getParameter("pageSize"));
    allCount = Integer.parseInt(request.getParameter("allCount"));
} catch (Exception e) {}

int allPage = (allCount - 1) / pageSize + 1 ;
 %>

            <input type="button" value="首页" <%=currentPage==1?"disabled":"" %> οnclick="changePage(1);">
                <input type="button" value="上一页" <%=currentPage==1?"disabled":"" %> οnclick="changePage(<%=currentPage-1 %>);">
                <input type="button" value="下一页" <%=currentPage==allPage?"disabled":"" %> οnclick="changePage(<%=currentPage+1 %>);">
                <input type="button" value="尾页" <%=currentPage==allPage?"disabled":"" %> οnclick="changePage(<%=allPage %>);">
                
                
                
                <form name="splitform" action="<%=url %>" method="post" οnsubmit="return check(this);">
                    <input type="submit" value="跳转到:"><input type="text" name="cp" value="<%=currentPage %>" size="4"> / <%=allPage %> 页
                    <%
                        // 最好定义一个数组,保存所有的选项
                        int[] allSelect = {2,5,10,20};
                     %>
                    每页显示 <select name="ps" οnchange="changePage(1);">
                        <%
                            for (int i = 0; i < allSelect.length;i++) {
                         %>
                        <option value="<%=allSelect[i] %>" <%=pageSize == allSelect[i]?"selected":"" %>><%=allSelect[i] %></option>
                        <%
                            }
                         %>
                    </select>
                    条数据 <br>
                    <%
                        if ("TRUE".equals(searchFlag)) {
                     %>
                    请输入查询关键字:<input type="text" name="keyword" value="<%=keyword %>"> <input type="submit" value="查询">
                    <%
                        }
                     %>
                </form>
        <script type="text/javascript">
            function check(myform) {
                var cp = myform.cp.value ;
                // 转换为数字类型
                cp = parseInt(cp);
                if (isNaN(cp)) {
                    return false ;
                } else {
                    if (cp < 1 || cp > "<%=allPage%>") {
                        return false ;
                    }                
                }
                return true ;
            }
            function changePage(cp) {
                // 接收传入的cp,并设置到隐藏的表单中。
                document.splitform.cp.value = cp ;
                // 提交表单
                document.splitform.submit();
            }
        </script>            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值