分页查询 Servlet+Jsp+PageBean

分页查询 Servlet+Jsp+PageBean

    • PageBean
    • Servlet
    • PageDao
    • Jsp
    • 简易流程图

PageBean

存放页面的相关信息,当前页,总记录数,总页数,每页显示行数,还有存放数据的集合。
把它们封装到一个类中。

package util;

import java.util.List;

public class PageBean<T> {
	private int currentPage = 1;// 当前页,默认为第一页
	
	private int pageCount = 3;// 每页显示的行数,默认显示3行
	
	private int totalCount;// 总记录数

	private int totalPage;// 总页数=总记录数/每页显示数||(+1)

	private List<T> pageData;//集合

	public int getTotalPage() {
		if (totalCount % pageCount == 0) {
			totalPage = totalCount / pageCount;
		} else {
			totalPage = totalCount / pageCount + 1;
		}
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public List<T> getPageData() {
		return pageData;
	}

	public void setPageData(List<T> pageData) {
		this.pageData = pageData;
	}
	
}

Servlet

在servlet层实现数据的查询,并把数据发送到Jsp。
在servlet中创建pageBean的实例,在servlet中调用dao层方法查询,并把pageBean作为参数传入。当查询结束后,pageBean的对象已经被装入数据,servlet再把查询后的结果,也就是封装在pageBean中的信息保存到域中发送到jsp页面。

package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.PageDao;
import entity.Users;
import service.impl.ServerEmp;
import util.PageBean;

/**
 * Servlet implementation class PageServlet2
 */
@WebServlet("/P2")
public class PageServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private PageDao user = new PageDao();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PageServlet2() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();

		// 获取page,第一次page为空
		String page = (String) request.getParameter("page");
		System.out.println("获取的page值" + page);

		// 如果为空,page=1
		if (page == "" || page == null) {
			page = "1";
		}

		// 转成整型
		int i = Integer.parseInt(page);
		System.out.println("当前页数为" + i);

		// 实例化PageBean对象,准备装入数据
		PageBean pd = new PageBean();
		// 当前页为i,为当前页重新赋值
		pd.setCurrentPage(i);
		System.out.println(pd.getCurrentPage());

		user.getALl(pd);// pageBean已经被Usersdao填充了数据

		// 将PaganBean有数据对象放进session中
		session.setAttribute("pd", pd);
		// 重定向页面
		response.sendRedirect("dealUsers.jsp");
		
		// RequestDispatcher rd = request.getRequestDispatcher("dealUsers.jsp");
		// rd.forward(request, response);
	}

	/**
	 * @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);
	}

}

PageDao

通过Servlet传过来的PageBean对象,得到当前页数,并进行判断,然后进行查询,将查询到的数据放到集合中,装配到PageBean对象。

package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Users;
import service.impl.ServerEmp;
import util.PageBean;

public class PageDao extends BaseDao<PageBean> {

	public void getALl(PageBean pd) {

		ServerEmp user = new ServerEmp();
		// 全部数据放入集合,得到集合长度,也就是总记录数
		List list1 = user.findAllUsers();
		int a = list1.size();

		// 赋值总记录数
		pd.setTotalCount(a);
		// 得到总页数 x
		int x = pd.getTotalPage();

		// 如果当前页<=0 ,令当前页page=1;
		if (pd.getCurrentPage() <= 0) {
			pd.setCurrentPage(1);
		}

		// 如果当前页>=最大页数 x,令当前页page=最大页数 x;
		if (pd.getCurrentPage() >= x) {
			pd.setCurrentPage(x);
		}

		// 获得当前页数,当作参数传入UsersDao的方法,得到数据
		int i = pd.getCurrentPage();
		// 调用业务逻辑层,传入参数 i,将得到数据放进集合list
		List<Users> list = user.getPageUsers(i, 3, i - 1);
		// 将集合放进PageBean对象pd中
		pd.setPageData(list);
	}

}


Jsp

将请求到的数据展现出来。

<%@ page language="java"
	import="java.util.*,java.sql.*,dao.EmpDao,service.impl.*,entity.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
	isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
div {
	position: absolute;
	left: 20%;
	top: 20%;
	line-height: 30px;
}
</style>
</head>
<body background="ywlr/static/images/cl.jpg">
	<div>
		<form>
			<table>
				<c:forEach items="${sessionScope.pd.pageData}" var="list"
					varStatus="status">
					<tr>
						<td>${list.rn}</td>
						<td>${list.username}</td>
						<td>${list.password}</td>
						<td>${list.id}</td>
					</tr>
				</c:forEach>

				<tr>
					<td><a href="P2?page=1">首页</a></td>
					<td><a href="P2?page=${sessionScope.pd.currentPage-1}">上一页</a></td>
					<td><a href="P2?page=${sessionScope.pd.currentPage+1}">下一页</a></td>
					<td><a href="P2?page=${sessionScope.pd.totalPage}">尾页</a></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>

简易流程图

PageDao Servlet Jsp 调用dao层方法返回数据,装配到PageBean对象中 返回数据到Jsp页面 PageDao Servlet Jsp

你可能感兴趣的:(Jsp,JSP)