jsp+servlet+javabean实现数据分页方法完整实例(3)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="java.sql.ResultSet"%> <%@page import="bean.PageProperties"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>简单数据分页</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link type="text/css" href="https://www.jb51.net/styles.css"> --> </head> <body> <table> <tr> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>分数</td> </tr> <% PageProperties pp=(PageProperties)request.getAttribute("result"); ResultSet rs=pp.getRs(); %> <% int i=1; while(rs.next()){ %> <tr> <td><%=rs.getObject(1) %></td> <td><%=rs.getObject(2) %></td> <td><%=rs.getObject(3) %></td> <td><%=rs.getObject(4) %></td> </tr> <% i++; if(i>10) break; } %> <br/> <span><%=pp.getTotalPages() %>页</span> <span>共<%=pp.getTotalRecords() %>条数据</span> <span>本页<%=i-1 %>条</span> <span>第<%=pp.getCurrentPage() %>页</span> <p> <% if ( pp.getCurrentPage() > 1 ) { %><a href="<%=path %>/page?page=<%=pp.getCurrentPage() - 1%>"><<上一页</a> <% } %> <% if ( pp.getCurrentPage() < pp.getTotalPages() ) { %><a href="<%=path %>/page?page=<%=pp.getCurrentPage() + 1%>">下一页>></a> <% } %> <input type="text" size="1" /> <input type="button" value="跳转"/> <script> function skip(){ var v=document.getElementById("input_text").value; location.href="page?page="+v; } </script> </p> </table> </body> </html>

初步看,感觉后台代码实在是繁琐,但这是考虑到程序健壮性与可移植性,方便代码重用。以后要用,根据自己的需要在属性文件(config.xml)中配置相关JDBC驱动,在jsp页面通过request获得后台Servlet(Page.jsp)的转发结果("result"),结合页面属性(PageProperties.java类)即可实现效果。

当然,这也是因为个人学习,倾向于多用点东西。

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

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