jsp分页操作过程

一、编写接口方法:

//pageSize:每页最多有多少行,pageNum:第几页数据
public List<Dept> getPaged(int pageSize,int pageNum) throws Exception;

二、编写实现类的方法

    @Override
    public List<Dept> getPaged(int pageSize, int pageNum) throws Exception {
        String sql = "select * from dept order by deptno limit ?,?";        
        Connection conn = DaoUtil.getConnection();
        ResultSet rs = DaoUtil.executeQuery(conn, sql,(pageNum-1)*pageSize,pageSize);
        
        int deptno;
        String dname;
        String loc;
        Dept dept = null;
        List<Dept> list = new ArrayList<Dept>();        
        while(rs.next()) {
            deptno = rs.getInt("deptno");
            dname = rs.getString("dname");
            loc = rs.getString("loc");
            
            dept = new Dept(deptno, dname, loc);
            list.add(dept);
        }        
        DaoUtil.closeConnection(conn);
        return list;
    }

三、编写Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageSize = 5;
        int pageNum = 1;
        //判断是否传递了页号
        if(request.getParameter("pageNum") != null) {
            pageNum = Integer.parseInt(request.getParameter("pageNum"));
        }
        
        DeptDao deptDao = new DeptDaoImpl();        
        try {
            //得到当前分页的数据
            List<Dept> list = deptDao.getPaged(pageSize, pageNum);
            request.setAttribute("list", list);
            //得到行数
            int rowCount = deptDao.getRowCount();
            
            int pageCount = (rowCount % pageSize == 0)?rowCount / pageSize:rowCount / pageSize+1;
            request.setAttribute("pageCount", pageCount);
            //设置当前页号
            request.setAttribute("currPageNum", pageNum);
            
            request.getRequestDispatcher("/dept/getPaged.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

四、编写jsp页

<table border="1" width="300">
        <c:forEach items="${ list }" var="dept">
            <tr>
                <td>${ dept.deptno }</td>
                <td>${ dept.dname }</td>
                <td>${ dept.loc }</td>
            </tr>
        </c:forEach>
        <tr>
            <td colspan="3">
                <c:forEach begin="1" end="3" var="pageNum">
                    <a href="${pageContext.request.contextPath }/GetPagedDeptServlet?pageNum=${ pageNum }">${ pageNum }</a>
                </c:forEach>            
            </td>
        </tr>
</table>

五、修改接口,添加方法

//得到总共有多少行数据
public int getRowCount() throws Exception;

六、编写实现类

    @Override
    public int getRowCount() throws Exception {
        String sql = "select count(*) from dept";
        Connection conn = DaoUtil.getConnection();
        ResultSet rs = DaoUtil.executeQuery(conn, sql);
        
        rs.next();
        
        int n = rs.getInt(1);
        
        DaoUtil.closeConnection(conn);
        return n;
    }

七、修改Servlet

//得到行数
int rowCount = deptDao.getRowCount();            
int pageCount = (rowCount % pageSize == 0)?rowCount / pageSize:rowCount / pageSize+1;
request.setAttribute("pageCount", pageCount);
request.getRequestDispatcher("/dept/getPaged.jsp").forward(request, response);

八、修改jsp页

<c:forEach begin="1" end="${ pageCount }" var="pageNum">

九、上一页、下一页
a)修改servlet

//设置当前页号
request.setAttribute("currPageNum", pageNum);

b)修改jsp页面

<tr>
            <td colspan="3">
                <c:forEach begin="1" end="${ pageCount }" var="pageNum">
                    <a href="${pageContext.request.contextPath }/GetPagedDeptServlet?pageNum=${ pageNum }">${ pageNum }</a>
                </c:forEach>
                
                <c:if test="${ currPageNum == 1 }">
                    <a>上一页</a>
                </c:if>
                <c:if test="${ currPageNum > 1 }">
                    <a href="${pageContext.request.contextPath }/GetPagedDeptServlet?pageNum=${ currPageNum - 1 }">上一页</a>
                </c:if>
                
                <c:if test="${ currPageNum == pageCount }">
                    <a>下一页</a>
                </c:if>                
                <c:if test="${ currPageNum < pageCount }">
                    <a href="${pageContext.request.contextPath }/GetPagedDeptServlet?pageNum=${ currPageNum + 1 }">下一页</a>
                </c:if>
                
            </td>
        </tr>


十、下拉列表框选择页号

<select id="page">
    <c:forEach begin="1" end="${ pageCount }" var="pageNum">
        <option value="${ pageNum }">${ pageNum }</option>
    </c:forEach>                    
</select>            
<input type="button" value="go" onclick="getPage()">

<script type="text/javascript">
function getPage(){
    var page = document.getElementById("page").value;//得到下拉列表框选中的页号
    location = "${pageContext.request.contextPath}/GetPagedDeptServlet?pageNum="+page;
}
</script>

十一、完整Servlet代码

public class GetPagedDeptServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageSize = 5;
        int pageNum = 1;
        //判断是否传递了页号
        if(request.getParameter("pageNum") != null) {
            pageNum = Integer.parseInt(request.getParameter("pageNum"));
        }
        
        DeptDao deptDao = new DeptDaoImpl();       
        try {
            //得到当前分页的数据
            List<Dept> list = deptDao.getPaged(pageSize, pageNum);
            request.setAttribute("list", list);
            //得到行数
            int rowCount = deptDao.getRowCount();
            
            int pageCount = (rowCount % pageSize == 0)?rowCount / pageSize:rowCount / pageSize+1;
            request.setAttribute("pageCount", pageCount);
            //设置当前页号
            request.setAttribute("currPageNum", pageNum);
            
            request.getRequestDispatcher("/dept/getPaged.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

源码及视频讲解下载地址:https://download.youkuaiyun.com/download/pcbhyy/10762875

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值