JavaWeb实现分页哦
分页是现在项目必备的功能,那我们来看看哦
1.首先分析分页需要哪些数据???
1.总记录数totalpageCount
sql语句 select count(*)from 表名
2.每页显示的数量 pageSize
1.客户端进行传递
2.由页面进行传递
3.总页数 totalPage
总记录数/每页显示的数量
注意:如果总记录数/每页的数量>0,则总页码+1
4.当前页currentPage
客户端进行传递的
5.每页的数据 <List>
通过sql语句求得:
select * from 表名 limit begin pageSize
begin可以由公式求得:(currentPage-1)*pageSizes
2.知道了数据之后,我们来写业务逻辑
业务逻辑:
1.编写page类
2.点击当前页的时候,需要传递两个参数:currentPage和pageSize
servlet页面:1.接受参数
2.调用service查询page
1.创建空的page对象
2.设置当前页面属性curentPage和每页显示的数量pageSize
3.调用dao查询总记录数pageTotalCount
4.调用dao查询list集合
(int begin,int pageSize)
5.计算总页码
总记录数/每页显示的数量
totalPage/pageSize
(dao层)
1.查询总记录数的方法
2.查询每页显示的数据的方法(int begin,int pageSize)
3.将page存入request
4.转发list.jsp显示
3.直接上代码
实体Page类
package com.cn.pojo;
import java.util.List;
public class Page<T> {
// 当前页
private int currentPage;
// 每一页的数量
private int pageSize;
// 总记录数
private int totalPageCount;
// 总页数
private int totalPage;
// 每一页的数据------泛型
private List<T> list;
public Page() {
super();
}
public Page(int currentPage, int pageSize, int totalPageCount, int totalPage, List<T> list) {
super();
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalPageCount = totalPageCount;
this.totalPage = totalPage;
this.list = list;
}
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;
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
servlet页面 BookPageServlet
package com.cn.servlet;
import java.io.IOException;
import java.sql.SQLException;
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.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import com.cn.pojo.Book;
import com.cn.pojo.Page;
import com.cn.service.BookService;
/**
* Servlet implementation class BookPageServlet
*/
public class BookPageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BookPageServlet() {
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");
//获得前端的值
String currentPage = request.getParameter("currentPage");
String pageSize = request.getParameter("pageSize");
//防止空指针异常
if(currentPage == null || "".equals(currentPage)) {
currentPage="1";
}
if(pageSize == null || "".equals(pageSize)) {
pageSize="5";
}
//存到request里
try {
Page<Book> page = BookService.findPage(currentPage, pageSize);
request.setAttribute("page", page);
//转发你想要的页面
request.getRequestDispatcher("bookall.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @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);
}
}
BookService层
public static Page<Book> findPage(String _currentPage,String _pageSize) throws SQLException{
//1.转化类型
int currentPage = Integer.valueOf(_currentPage);
int pageSize = Integer.valueOf(_pageSize);
//2.new对象
Page<Book> page = new Page<>();
//3.赋值当前页 每一页的数量
page.setCurrentPage(currentPage);
page.setPageSize(pageSize);
//4.计算总记录数
int totalPageCount = JDBCDemo.totalPageCount();
page.setTotalPageCount(totalPageCount);
//5.计算总页数
int totalPage = totalPageCount % pageSize == 0 ?(totalPageCount/pageSize):(totalPageCount/pageSize+1);
page.setTotalPage(totalPage);
//6.每一页的数据
int begin = (currentPage-1)*pageSize+1;
List<Book> list = JDBCDemo.page(begin, pageSize);
page.setList(list);
return page;
}
我直接写的JDBC,没有实现接口哦~
//计算总记录数
public static int totalPageCount() throws SQLException {
String sql = "select count(*) from book_table";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet set = statement.executeQuery();
int totalPageCount = 0;
//判断是否有内容
while(set.next()) {
//获取当前的内容
totalPageCount = set.getInt(1);
}
return totalPageCount;
}
//每一页的数据
public static List<Book> page(int begin,int pageSize) throws SQLException {
String sql = "select * from book_table limit ?,?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, begin);
statement.setInt(2, pageSize);
List<Book> list = new ArrayList<Book>();
ResultSet set = statement.executeQuery();
Book book = null;
while (set.next()) {
book = new Book();
book.setBookNum(set.getInt("book_num"));
book.setBookName(set.getString("book_name"));
book.setBookAuthor(set.getString("book_author"));
book.setBookPrice(set.getInt("book_price"));
book.setBookPress(set.getString("book_press"));
book.setBookIntroduction(set.getString("book_introduction"));
list.add(book);
}
return list;
}
因为我转发到bookall.jsp页面,这里是我的bookall.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<title>Insert title here</title>
</head>
<body>
<table border="3">
<tr>
<th>图书编号</th>
<th>图书名字</th>
<th>图书作者</th>
<th>图书价格</th>
<th>图书出版社</th>
<th>图书介绍</th>
<th>操作</th>
</tr>
//page存到request里了
<c:forEach items="${page.list}" var = "book" >
<tr>
<td>${book.bookNum}</td>
<td>${book.bookName}</td>
<td>${book.bookAuthor}</td>
<td>${book.bookPrice}</td>
<td>${book.bookPress}</td>
<td>${book.bookIntroduction}</td>
<td>
<a href = "InforServlet?bookNum=${book.bookNum}">修改</a>
<a href = "DeleteeServlet?bookNum=${book.bookNum}">删除</a>
</td>
</tr>
</c:forEach>
</table>
//上一页 = 当前页-1
<c:if test="${page.currentPage != 1}">
<a style="color:red" href="BookPageServlet?currentPage=${page.currentPage-1}&pageSize=5">上一页</a>
</c:if>
//循环输出每一页
<c:forEach begin="1" end="${page.totalPage}" var="i">
<c:if test="${page.currentPage == i}">
//如果点击当前页,就让页码变红色,其余的不变颜色
//href要把地址带过来,否则你的数据不会更新
<a style="color:red" href="BookPageServlet?currentPage=${i}&pageSize=5">${i}</a>
</c:if>
//下一页 = 当前页+1
<c:if test="${page.currentPage != i}">
<a href="BookPageServlet?currentPage=${i}&pageSize=5">${i}</a>
</c:if>
</c:forEach>
<c:if test="${page.currentPage != page.totalPage}">
<a style="color:red" href="BookPageServlet?currentPage=${page.currentPage+1}&pageSize=5">下一页</a>
</c:if>
总共${page.totalPageCount}条数据,总共${page.totalPage}页
</body>
</html>
下图就是服务启动之后的页面展示...
以上就是javaWeb的分页哦,希望可以对大家有所帮助啦
|