JSP实现分页效果

咱们在浏览网页的时候,当一个页面的数据不足以展示完全所有的内容,一般都涉及到分页,下一页的功能该怎么实现呢?首先我们来分析一下:

JSP实现分页效果

那么直接上代码:

这里需要备注一下,本次的代码是在对三层优化之后进行操作的,所以我先把数据访问层的重构代码贴出来:

package org.ThreeLayer.DButil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.ThreeLayer.Entity.Student; public class DButil { public static final String driver = "com.mysql.cj.jdbc.Driver"; public static final String url = "jdbc:mysql://localhost:3306/zxy?&useSSL=false&serverTimezone=UTF-8&useSSL=false&serverTimezone = GMT"; public static final String username = "root"; public static final String password = "zxy170518."; public static Connection connection = null;//链接数据库 public static PreparedStatement pstmt=null;//执行sql语句 public static ResultSet rs=null; public static Connection getConnection() throws SQLException, ClassNotFoundException { Class.forName(driver); return DriverManager.getConnection(url,username,password); } public static int getTotalCount(String sql) { int count=0; try { pstmt=createPrepareStatement(sql, null); rs=pstmt.executeQuery(); if(rs.next()) { count=rs.getInt(1); } }catch(SQLException e) { e.printStackTrace(); }catch(ClassNotFoundException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { closeAll(connection, pstmt, rs); } return count; } public static PreparedStatement createPrepareStatement(String sql,Object[] obj) throws ClassNotFoundException, SQLException { pstmt=getConnection().prepareStatement(sql); if(obj!=null) { for(int i=0;i<obj.length;i++) { pstmt.setObject(i+1, obj[i]);//进行更新动作 } } return pstmt; } public static boolean UpdateSQL(String sql,Object[] obj) { try { pstmt=createPrepareStatement(sql, obj); int count=pstmt.executeUpdate(); if(count>0) { return true; } else { return false; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; }finally { closeAll(connection,pstmt,rs); } } public static ResultSet FindSQL(String sql,Object[] obj) { try { pstmt=createPrepareStatement(sql, obj); rs=pstmt.executeQuery(); return rs; }catch(ClassNotFoundException e) { e.printStackTrace(); return rs; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return rs; }catch(Exception e) { e.printStackTrace(); return rs; } } public static void closeAll(Connection connection,PreparedStatement pstmt,ResultSet rs) { try { if(connection!=null); connection.close(); if(pstmt!=null); pstmt.close(); if(rs!=null); rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); } } }

基本上就是普通的数据库操作功能,很好懂,就不多解释了;
对于数据访问层的Dao:

public int getTotalCount()//查询数据总数 { String sql="select count(1) from student"; return DButil.getTotalCount(sql); } public List<Student> findStudentByPage(int currentPage,int pageSize)//currentPage:当前页数;pageSize页面所能容纳的最大数据量 { String sql="select * from student limit ? , ?"; Object[] obj= {currentPage*pageSize,pageSize}; List<Student> students=new ArrayList<>(); ResultSet rs=DButil.FindSQL(sql, obj); try { while(rs.next()) { Student student=new Student(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4)); students.add(student); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return students; }

对于业务逻辑层:

Server:

public int getTotalCount() { return studentdao.getTotalCount(); } public List<Student> findStudentByPage(int currentPage,int pageSize) { return studentdao.findStudentByPage(currentPage, pageSize); }

对于视图层的后台代码:

Servlet:

package org.Three.Servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.ThreeLayer.Entity.Page_S; import org.ThreeLayer.Entity.Student; import org.ThreeLayer.Server.Student_Server; public class findStudentByPage extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Student_Server studentS=new Student_Server(); // int currentPage=2; Page_S pag=new Page_S(); String tmp=request.getParameter("currentPage"); if(tmp==null)//判断是否为第一次进行访问 { tmp="0"; } int sum=studentS.getTotalCount(); pag.setTotalCount(sum); int currentPage= Integer.parseInt(tmp); pag.setCurrentPage(currentPage); String tmp2=request.getParameter("choose"); if(tmp2==null)//默认一页3个内容 { tmp2="3"; } int pageSize=Integer.parseInt(tmp2); pag.setPageSize(pageSize); List<Student> students =studentS.findStudentByPage(currentPage, pageSize); pag.setStudents(students); request.setAttribute("pag", pag); request.getRequestDispatcher("index.jsp").forward(request, response); System.out.print(students); System.out.print(sum); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

还有一个实体类:Page:

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

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