前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分页-代码实现

分页-代码实现

作者头像
星哥玩云
发布2022-09-14 21:12:06
1.3K0
发布2022-09-14 21:12:06
举报
文章被收录于专栏:开源部署

1、分页数据流转流程图

PageBean.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:forward page="/frame.jsp" />

frame.jsp

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
		}
	}
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、分页数据流转流程图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档