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:

package org.threelayer.entity;

import java.util.list;

public class page_s {//为了不出现于重名,改了一下
 private int currentpage;
 private int pagesize;//页面大小,即页面数据个数
 private int totalcount;//总数据
 private int totalpage;//总页数
 private list<student> students;
 public page_s() {
 }
 public page_s(int currentpage, int pagesize, int totalcount, int totalpage, list<student> students) {
 this.currentpage = currentpage;
 this.pagesize = pagesize;
 this.totalcount = totalcount;
 this.totalpage = totalpage;
 this.students = students;
 }
 public int getcurrentpage() {
 return currentpage;
 }
 public void setcurrentpage(int currentpage) {
 this.currentpage = currentpage;
 }
 public int getpagesize() {
 return pagesize;
 }
 public void setpagesize(int pagesize) {
 this.pagesize = pagesize;
 this.totalpage=this.totalcount%this.pagesize==0?this.totalcount/this.pagesize:this.totalcount/this.pagesize+1;
 }
 public int gettotalcount() {
 return totalcount;
 }
 public void settotalcount(int totalcount) {
 this.totalcount = totalcount;
 }
 public int gettotalpage() {
 return totalpage;
 }
 public void settotalpage(int totalpage) {
 this.totalpage = totalpage;
 }
 public list<student> getstudents() {
 return students;
 }
 public void setstudents(list<student> students) {
 this.students = students;
 }
}

最后贴上index.jsp:

<%@page import="java.util.list"%>
<%@page import="org.threelayer.entity.student"%>
<%@page import="org.threelayer.entity.page_s"%>
<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>学生信息管理</title>
</head>
<body>
 <table border=1px>
 <tr>
 <th>学号</th>
 <th>姓名</th>
 <th>性别</th>
 <th>操作</th>
 </tr>
 <%
 page_s pagg=(page_s)request.getattribute("pag");
 for(student student:pagg.getstudents())
 {
 %>
 <tr>
 <th><a href="findstudentbyid_servlet?uid=<%=student.getid()%>" ><%=student.getid() %></a></th>
 <th><%=student.getname() %></th>
 <th><%=student.getsex() %></th>
 <th><a href="deletestudent_servlet?uid=<%=student.getid()%>" >删除</a></th>
 </tr>
 <%
 }
 %>
 </table>
 <a href="add.jsp" >增加</a>
 <%
 if(pagg.getcurrentpage()==0)//用户位于首页的时候
 {
 %> 
 <a href="findstudentbypage?currentpage=<%=pagg.getcurrentpage()+1%>" >下一页</a>
 <a href="findstudentbypage?currentpage=<%=pagg.gettotalpage()-1%>" >尾页</a>
 <% 
 }else if(pagg.getcurrentpage()==pagg.gettotalpage()-1)//用户位于尾页的时候
 {
 %>
 <a href="findstudentbypage?currentpage=0" >首页</a>
 <a href="findstudentbypage?currentpage=<%=pagg.getcurrentpage()-1%>" >上一页</a>
 <% 
 }else//用户位于中间页面的时候
 {
 %> <a href="findstudentbypage?currentpage=0" >首页</a>
 <a href="findstudentbypage?currentpage=<%=pagg.getcurrentpage()+1%>" >下一页</a>
 <a href="findstudentbypage?currentpage=<%=pagg.getcurrentpage()-1%>" >上一页</a>
 <a href="findstudentbypage?currentpage=<%=pagg.gettotalpage()-1%>" >尾页</a>
 <%
 }
 %>
 
 <br>
</body>
</html>

看一下效果图:

首先看数据库内容:

然后是首页:

下一页:

最后是尾页:

总的说明一下:

首先对于功能的阐述,第一步计算总的数据量,然后规定默认容量大小为3,最终在jsp代码中加上跟用户进行交互的功能,即让用户选择一页多少内容(由于我写的那个有点bug,就先不贴,等后面自己能完美实现之后,再更新),之后对前端数据进行打包,要思考的是,我们对于这个功能我们所需要的数据有哪些呢?首先,总数据量要吧?然后要存放总的数据内容吧?然后页面大小需要吧?然后用户所在页面的那个页面位置的数要吧?最后一个就是通过总数据量和页面大小计算出来的总页面数也需要吧?所以,一共就需要记录5个属性值,那就打包成一个javabean吧,前面代码也贴出来了。最后要提一点,对于如果我第一次进行访问页面的时候,我应该是有一些属性值是为null的,这样是会报空指针异常的,那么就要进行一些小小的处理,哪些呢?比如如果用户第一次进行访问,系统是收不到用户当前所在页面的页面数值的,那么就要判断一下,(此处上代码)如果是第一次进行访问,那么就给与一个默认值0,也就是第一页,那么就处理好了这个小问题了,诸如此类问题还有就是用户在进行选择一页多少内容的时候,也是需要进行赋予一个默认值的,不然也会报空指针。然后对于web.xml文件内容的设置,首页应该设置为实现分页功能的servlet,因为你每做一次翻页或者首次访问,虽然都是在index.jsp中,但是你需要把每次做完动作之后得到的新的内容进行请求转发,这样才能实现更新,不然程序会报错。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。

相关文章