自己写的一个分页

本文详细介绍了一种使用JSP实现网页分页的方法,包括页面布局、JavaScript分页逻辑及后端数据处理流程。通过具体代码示例展示了如何设置每页记录数、计算总页数,并实现前后页跳转等功能。

TurnPage.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<table align="left" width="100%" border="0" class="midTable1"
    cellpadding="3" cellspacing="0">
    <tr>
        <td width="45%" class="midTable1td1">
            每页显示十条记录,总页数:<font color="red">${totalPage}</font>
            当前页:&nbsp;<font color="red">${pageNo }</font>
            <input type="hidden" name="pageNo" value="${pageNo }">
        </td>
        <td class="midTable1td1" align="center">
            <INPUT type="Button" class=button value="首页"
                onclick="first(${pageNo})" />
            &nbsp;
            <INPUT type="Button" class=button value="上一页"
                onclick="previous(${pageNo })" />
            &nbsp;
            <INPUT type="Button" class=button value="下一页"
                onclick="next(${pageNo },${totalPage})" />
            &nbsp;
            <INPUT type="Button" class=button value="末页"
                onclick="last(${pageNo },${totalPage})" />
        </td>
    </tr>
</table>

 

=====================================================================================

要用到分页的JSP页面:

 

<script language="JavaScript">
      
        //分页 第一页
        function first(pageNo){
             if(pageNo!=1){
                 document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo=1";
                 document.fm_msgInfo.submit();
             }else{
                 alert("这已经是第一页");
             }
        }
        //分页 前一页
         function previous(pageNo){
             if(pageNo>1){
                 pageNo-=1;
                 document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo="+pageNo;
                 document.fm_msgInfo.submit();
             }else{
                 alert("这已经是第一页");
             }
         }
         //分页 下一页
         function next(pageNo,totalPage){
             if(pageNo<totalPage){
                 pageNo+=1;
                 document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo="+pageNo;
                 document.fm_msgInfo.submit();
             }else{
             alert("这已经是最后一页");
             }
         }
         //分页 最后一页
         function last(pageNo,totalPage){
             if(pageNo!=totalPage){
                 document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo="+totalPage;
                 document.fm_msgInfo.submit();
             }else{
                 alert("这已经是最后一页");
             }
        }
    </script>

 

 

<body>

 

 

 

在这里引入TurnPage.jsp

 

<tr>
      <jsp:include page="../frame/TurnPage.jsp"/>
</tr>

 

</body>

 

===================================================================================

 

String temp_pageNo = request.getParameter("pageNo");
            if(temp_pageNo==null){
                temp_pageNo="1";
            }
            Integer pageNo = 1;
            Integer totalPage = 1;
            try {
                pageNo = Integer.parseInt(temp_pageNo);
            } catch (Exception e) {
                //
            }
            try {
                MsgDao dao = new MsgDao();
                String userID=(String) httpSession.getAttribute("userID");
                //设置每页显示的记录数为 10
                int page_record=10;
                //获取总页数
                totalPage = (dao.getRows(userID)+ page_record-1)/page_record;
                //System.out.println(totalPage);
                //设置总页数到session
                httpSession.setAttribute("totalPage", totalPage);
                // 删除的是哪一页就显示哪一页 如被删页只有一条数据 就显示最后一页的数据
                if (totalPage < pageNo) {
                    pageNo = totalPage;
                }
                //查询第 pageNo 页,每页 page_record 条记录 的所有 userID 的短信列表
                List msgList=dao.getAllMsg(pageNo,page_record,userID);
               
                httpSession.setAttribute("pageNo", pageNo);

                httpSession.setAttribute("msgList", msgList);
                return mapping.findForward("msgInfo");

 

 

 

===================================================================================

DAO:

 

//要查的第pageNo不能为负数

            if(pageNo<1){
                pageNo=1;
            }
            Integer notInTop=(pageNo-1)*page_record;
            String sql="";
            sql="select top "+page_record+" * from msgInfo where msgID not in(select top "+notInTop+" msgID from msgInfo where userID=? order by msgID desc) and userID=? order by msgID desc";
            //System.out.println(sql);
            ps=con.prepareStatement(sql);
            ps.setString(1,userID);
            ps.setString(2,userID);
           
            ResultSet rs=ps.executeQuery();

 

//查出来之后把它放到bean里,再把bean放到一个List里,最后把一个List返回就行了

 

while(rs.next()){
                Msg bean=new Msg();
                bean.setMsgID(Integer.toString(rs.getInt(1)));
                bean.setPrivyName(rs.getString(2));
               
                al.add(bean);
            }            

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值