阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

分页-代码实现

91次阅读
没有评论

共计 6505 个字符,预计需要花费 17 分钟才能阅读完成。

1、分页数据流转流程图

分页 - 代码实现

PageBean.java

import java.util.List; public class PageBean<T> {private List<T> beanList;// 当前页记录数, 需要传递 private int tr;// 总记录数, 需要传递 private int pc;// 当前页码, 需要传递 private int ps;// 每页记录数, 需要传递 private int tp;// 总页数, 计算 // 其他的提供 get/set 方法 // 但 tp 只提供 get 方法 public int getTp(){ tp=tr/ps; return tr%ps==0?tp:tp+1; } public List<T> getBeanList() {return beanList; } public void setBeanList(List<T> beanList) {this.beanList = beanList; } public int getTr() {return tr; } public void setTr(int tr) {this.tr = tr; } public int getPc() {return pc; } public void setPc(int pc) {this.pc = pc; } public int getPs() {return ps; } public void setPs(int ps) {this.ps = ps; } }

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <jsp:forward page="/frame.jsp" />

frame.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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 rel="stylesheet" type="text/css" href="styles.css"> --> </head> <frameset rows="20%,*"> <frame src="<c:url value='/top.jsp'/>" name="top"/> <frame src="<c:url value='/welcome.jsp'/>" name="main"/> </frameset> </html>

top.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <!-- 它的作用是为本页面所有的表单和超链接指定显示内容的框架!--> <base target="main"> <title>My JSP 'top.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"> --> </head> <body style="text-align: center;"> <a href="<c:url value='/CustomerServlet'/>"> 查询客户 </a> </body> </html>

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'welcome.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"> --> </head> <body> <h1 align="center"> 欢迎登录本系统 </h1> </body> </html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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 rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3 align="center"> 客户列表 </h3> <table border="1" width="70%" align="center"> <tr> <th> 客户姓名 </th> <th> 性别 </th> <th> 生日 </th> <th> 手机 </th> <th> 邮箱 </th> <th> 描述 </th> <th> 操作 </th> </tr> <c:forEach items="${pb.beanList}" var="cstm"> <tr> <td>${cstm.cname}</td> <td>${cstm.gender}</td> <td>${cstm.birthday}</td> <td>${cstm.cellphone}</td> <td>${cstm.email}</td> <td>${cstm.description}</td> <td> <a href="#"> 编辑 </a> <a href="#"> 删除 </a> </td> </tr> </c:forEach> </table> </br> <!-- 给出分页相关的链接 --> <center> <a href='<c:url value="/CustomerServlet"/>'> 首页 </a> <a href='<c:url value="/CustomerServlet?pc=${pb.pc-1}"/>'> 上一页 </a> <a href='<c:url value="/CustomerServlet?pc=${pb.pc+1}"/>'> 下一页 </a> <a href='<c:url value="/CustomerServlet?pc=${pb.tp}"/>'> 尾页 </a> </center> </body> </html>

CustomerServlet.java

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CustomerServlet */ public class CustomerServlet extends HttpServlet {private static final long serialVersionUID = 1L; CustomerDao customerDao = new CustomerDao(); /** * @see HttpServlet#HttpServlet() */ public CustomerServlet() {super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/* * 1、获取页面传递的 pc * 2、给定 ps 的值 * 3、使用 pc 和 ps 调用 service 方法,得到 PageBean, 保存到 request 域 * 4、转发到 list.jsp * */ /* * 1. 得到 pc * 如果 pc 参数不存在,说明 pc=1 * 如果 pc 参数存在,需要转换成 int 类型即可 * */ int pc = getPc(request); int ps = 10; PageBean pb = customerDao.findAll(pc,ps); request.setAttribute("pb", pb); request.getRequestDispatcher("/list.jsp").forward(request, response); } public int getPc(HttpServletRequest request) {String value = request.getParameter("pc"); if(value==null || value.trim().isEmpty()) {return 1; } return Integer.parseInt(value); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet(request, response); } }

CustomerDao.java

import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.mchange.v2.c3p0.ComboPooledDataSource; public class CustomerDao {private QueryRunner qr = new QueryRunner(new ComboPooledDataSource()); /** * 添加客户 * * @param c */ public void add(Customer c) {try {String sql = "insert into t_customer values(?,?,?,?,?,?,?)"; Object[] params = { c.getCid(), c.getCname(), c.getGender(), c.getBirthday(), c.getCellphone(), c.getEmail(), c.getDescription()}; qr.update(sql, params); } catch(SQLException e) {throw new RuntimeException(e); } } public PageBean findAll(int pc,int ps) {try {/* * 创建 PageBean 对象 pb * 设置 pb 的 pc 和 ps * 得到 tr, 给 pb 设置 tr * 得到 beanList,设置给 pb * */ PageBean<Customer> pb = new PageBean<Customer>(); pb.setPc(pc); pb.setPs(ps); /* * 得到 tr * */ String sql = "select count(*) from t_customer"; Number num = (Number)qr.query(sql, new ScalarHandler()); int tr = num.intValue(); pb.setTr(tr); /* * 得到 beanList * */ sql = "select * from t_customer limit ?,?"; List<Customer> beanList = qr.query(sql, new BeanListHandler<Customer>(Customer.class), (pc-1)*pb.getPs(), pb.getPs()); pb.setBeanList(beanList); return pb; } catch(SQLException e) {throw new RuntimeException(e); } } }

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-06-06发表,共计6505字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中