Java Web 简单的分页显示实例代码
程序员文章站
2024-03-12 23:29:45
本文通过两个方法:(1)计算总的页数。 (2)查询指定页数据,实现简单的分页效果。
思路:首先得在 dao 对象中提供分页查询的方法,在控制层调用该方法查到指定页的数据,...
本文通过两个方法:(1)计算总的页数。 (2)查询指定页数据,实现简单的分页效果。
思路:首先得在 dao 对象中提供分页查询的方法,在控制层调用该方法查到指定页的数据,在表示层通过 el 表达式和 jstl 将该页数据显示出来。
先给大家展示下效果图:
题外话:该分页显示是用 “表示层-控制层-dao层-数据库”的设计思想实现的,有什么需要改进的地方大家提出来,共同学习进步。废话不多说了,开始进入主题,详细步骤如下所示:
1.dao层-数据库
jdbcutils 类用于打开和关闭数据库,核心代码如下:
import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; public class jdbcutils { private connection conn=null; private preparedstatement pstmt=null; /** * connect 连接数据库 * @return */ public connection connect(){ string user="root"; string password="1234"; string driverclass = "com.mysql.jdbc.driver"; string jdbcurl = "jdbc:mysql://localhost:3306/book"; try { class.forname(driverclass); conn = drivermanager.getconnection(jdbcurl, user, password); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return conn; } /** * close 关闭数据库 * @param conn * @param pstmt * @param resu */ public void close(connection conn,preparedstatement pstmt,resultset result){ if(conn != null){ try { conn.close(); } catch (sqlexception e) { // todo auto-generated catch block } } if(pstmt != null){ try { pstmt.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } if(result != null){ try { result.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
userdao 类中的方法 getpage() 和方法 listuser() 分别用来计算总页数和查询指定页的数据,核心代码如下:
import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import com.db.jdbcutils; public class userdao { /** * 计算总的页数 * @return */ public int getpage(){ int recordcount=0,t1=0,t2=0; preparedstatement pstmt=null; resultset result=null; jdbcutils jdbc=new jdbcutils(); connection conn=jdbc.connect(); string sql="select count(*) from books"; try { pstmt=conn.preparestatement(sql); result=pstmt.executequery(); result.next(); recordcount=result.getint(1); t1=recordcount%5; t2=recordcount/5; } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); }finally{ jdbc.close(conn, pstmt, result); } if(t1 != 0){ t2=t2+1; } return t2; } /** * 查询指定页的数据 * @param pageno * @return */ public list<user> listuser(int pageno){ preparedstatement pstmt=null; resultset result=null; list<user> list=new arraylist<user>(); int pagesize=5; int page=(pageno-1)*5; jdbcutils jdbc=new jdbcutils(); connection conn=jdbc.connect(); string sql="select * from books order by id limit ?,?"; try { pstmt=conn.preparestatement(sql); pstmt.setint(1, page); pstmt.setint(2, pagesize); result=pstmt.executequery(); while(result.next()){ user user=new user(); user.setid(result.getint(1)); user.setname(result.getstring(2)); user.setnumber(result.getstring(3)); list.add(user); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); }finally{ jdbc.close(conn, pstmt, result); } return list; } }
user 类用于存储查询到的数据,核心代码如下:
public class user { private int id; private string name; private string number; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getnumber() { return number; } public void setnumber(string number) { this.number = number; } }
2.控制层
listuser 类内部调用 userdao 对象查询数据并指派页面显示数据,核心代码如下:
import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dao.user; import com.dao.userdao; public class listuser extends httpservlet { public listuser() { super(); } public void destroy() { super.destroy(); // just puts "destroy" string in log // put your code here } public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request, response); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcharacterencoding("utf-8"); int pageno = 1; userdao userdao=new userdao(); list<user> lists=new arraylist<user>(); string pageno=request.getparameter("pagenos"); if(pageno != null){ pageno=integer.parseint(pageno); } lists=userdao.listuser(pageno); int recordcount=userdao.getpage(); request.setattribute("recordcount", userdao.getpage()); request.setattribute("listss", lists); request.setattribute("pagenos", pageno); request.getrequestdispatcher("userlist.jsp").forward(request, response); } public void init() throws servletexception { // put your code here } }
3.表示层
输出页面 userlist.jsp ,使用 el 和 jstl 输出查询结果,核心代码如下:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'userlist.jsp' starting page</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 rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> th,td{width: 150px;border: 2px solid gray;text-align: center;} body{text-align: center;} a{text-decoration: none;} table {border-collapse: collapse;} </style> </head> <body> <h2 align="center">图书信息</h2> <table align="center"> <tr><td>书号</td><td>书名</td><td>库存量</td></tr> </table> <table align="center"> <c:foreach items="${listss}" var="person"> <tr> <td class="hidden-480">${person.id}</td> <td class="hidden-480">${person.name }</td> <td class="hidden-480">${person.number }</td> </tr> </c:foreach> </table> <br> <c:if test="${pagenos>1 }"> <a href="listuser?pagenos=1" >首页</a> <a href="listuser?pagenos=${pagenos-1 }">上一页</a> </c:if> <c:if test="${pagenos <recordcount }"> <a href="listuser?pagenos=${pagenos+1 }">下一页</a> <a href="listuser?pagenos=${recordcount }">末页</a> </c:if> <form action="listuser"> <h4 align="center">共${recordcount}页   <input type="text" value="${pagenos}" name="pagenos" size="1">页 <input type="submit" value="到达"> </h4> </form> </body> </html>
以上所述是小编给大家介绍的java web 简单的分页显示实例代码,希望对大家有所帮助
上一篇: Java线程池框架核心代码解析
下一篇: 很简单的Java断点续传实现原理