Jsp真分页实例(2)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String page = null; page = request.getParameter("page"); if(page == null || page=="") page = "1"; StudentDao studao = new StudentDao(); request.setAttribute("student",studao.findByPage(Integer.parseInt(page))); request.setAttribute("pagenum",studao.userCount()/5+1);//总页数 request.setAttribute("page", page);//当前页 request.getRequestDispatcher("student.jsp").forward(request, response); }

前台JSP代码:

<table cellpadding="2" cellspacing="0"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>电话</th> <th>成绩</th> </tr> </thead> <c:forEach items="${student}" var="st"> <tr> <td>${st.getId()}</td> <td>${st.getName()}</td> <td>${st.getAge()}</td> <td>${st.getPhone()}</td> <td>${st.getScore()}</td> </tr> </c:forEach> </table> <br> 共 ${pagenum}页 当前 第${page}页 <c:choose> <c:when test="${page>1}"> <a href="getSutent?page=${page-1}" ><input type="button" value="上一页" ></a> </c:when> <c:otherwise> <input type="button" value="上一页" disabled="disabled" /> </c:otherwise> </c:choose> <c:choose> <c:when test="${page!=pagenum}"> <a href="getSutent?page=${page+1}" ><input type="button" value="下一页"></a> </c:when> <c:otherwise> <input type="button" value="下一页" disabled="disabled" /> </c:otherwise> </c:choose>

本例是真分页的一个简单实现,有着明显的缺点。

例如:

1.在后台相关业务逻辑处,只对page做了简单的判断,因为查询相关page时,参数是写入前台a标签中的,所以懂技术的用户,可以随意改动其值

由此查询数据库可能带来意想不到的错误。

2.功能不够完善,仅提供了上一页下一页按钮的简单功能。

另外:实现假分页时可以结合ajax和json。以此可实现无刷新翻页,看起来功能和真分页一样。。。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjfpg.html