当前位置:网站首页>通用分页(2)
通用分页(2)
2022-06-30 02:58:00 【安离九歌】
目录
前言
上次我们分享了通用分页(1),今天分享的内容是通用分页(2)。本文的内容基于上一篇文章
一、pageBean的完善
分析:
对pagebean进行初始化
初始化jsp页面传递过来的当前页
初始化jsp页面传递过来的页大小
初始化jsp页面传递过来是否分页
保留上一次的查询请求
保留上一次的查询条件
package com.zhw.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
private String url;
//保存上一次查询条件
private Map<String, String[]> parmeterMap = new HashMap<String, String[]>();
//获取最大页码
public int maxPage() {
return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1;
}
//获取上一页的页码
public int previousPage() {
return this.page > 1 ? this.page -1 : this.page;
}
//获取下一页的页码
public int nextPage() {
return this.page<this.maxPage() ? this.page +1 : this.page;
}
//初始化pagebean
public void setRequest(HttpServletRequest req) {
this.setPage(req.getParameter("page"));
this.setRows(req.getParameter("rows"));
this.setPagination(req.getParameter("pagination"));
this.setUrl(req.getRequestURL().toString());
this.setParmeterMap(req.getParameterMap());
}
private void setPage(String page) {
if(StringUtils.isNotBlank(page)) {
//set自动生成的方法
this.setPage(Integer.valueOf(page));
}
}
private void setPagination(String pagination) {
if(StringUtils.isNotBlank(pagination)) {
this.setPagination(!"false".equals(pagination));
}
}
private void setRows(String rows) {
if(StringUtils.isNotBlank(rows)) {
this.setRows(Integer.valueOf(rows));
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, String[]> getParmeterMap() {
return parmeterMap;
}
public void setParmeterMap(Map<String, String[]> parmeterMap) {
this.parmeterMap = parmeterMap;
}
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination
+ ", url=" + url + ", parmeterMap=" + parmeterMap + "]";
}
}
二、PageTag的实现
package com.zhw.tag;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.zhw.util.PageBean;
public class PageTag extends BodyTagSupport{
private PageBean pagebean;
public PageBean getPagebean() {
return pagebean;
}
public void setPagebean(PageBean pagebean) {
this.pagebean = pagebean;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private Object toHTML() {
StringBuffer sb = new StringBuffer();
sb.append("<form action='"+pagebean.getUrl()+"' id='pageBeanForm' method='post'>");
sb.append(" <input type='hidden' name='page'>");
Map<String, String[]> parmeterMap = pagebean.getParameterMap();
if(parmeterMap != null && parmeterMap.size()>0) {
Set<Entry<String, String[]>> entrySet = parmeterMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
String key = entry.getKey();
String[] value = entry.getValue();
if(!"page".equals(key)) {
for (String string : value) {
sb.append(" <input type='hidden' name='"+key+"' value='"+value+"'>");
}
}
}
}
sb.append("</form>");
sb.append("<ul class='pagination justify-content-center'>");
sb.append(" <li class='page-item'><a class='page-link'");
sb.append(" href='javascript:gotoPage(1)'>首页</a></li>");
sb.append(" <li class='page-item'><a class='page-link'");
sb.append(" href='javascript:gotoPage("+pagebean.previousPage()+")'><</a></li>");
sb.append(" <li class='page-item'><a class='page-link' href='#'>"+pagebean.getPage()+"</a></li>");
sb.append(" <li class='page-item "+(pagebean.getPage() == pagebean.maxPage() ? "disabled" : "")+"'><a class='page-link' href='javascript:gotoPage("+pagebean.nextPage()+")'>></a></li>");
sb.append(" <li class='page-item "+(pagebean.getPage() == pagebean.maxPage() ? "disabled" : "")+"'><a class='page-link' href='javascript:gotoPage("+pagebean.maxPage()+")'>尾页</a></li>");
sb.append(" <li class='page-item go-input'><b>到第</b><input class='page-link'");
sb.append(" type='text' id='skipPage' name='' /><b>页</b></li>");
sb.append(" <li class='page-item go'><a class='page-link'");
sb.append(" href='javascript:skipPage()'>确定</a></li>");
sb.append(" <li class='page-item'><b>共"+pagebean.getTotal()+"条</b></li>");
sb.append("</ul>");
sb.append("<script type='text/javascript'>");
sb.append(" function gotoPage(page) {");
sb.append(" document.getElementById('pageBeanForm').page.value = page;");
sb.append(" document.getElementById('pageBeanForm').submit();");
sb.append(" }");
sb.append(" function skipPage() {");
sb.append(" var page = document.getElementById('skipPage').value;");
sb.append(" if (!page || isNaN(page) || parseInt(page) < 1");
sb.append(" || parseInt(page) > "+pagebean.maxPage()+") {");
sb.append(" alert('请输入1~"+pagebean.maxPage()+"的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append(" }");
sb.append("</script>");
return sb;
}
}
Servlet:
package com.zhw.web;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 com.zhw.dao.BookDao;
import com.zhw.entity.Book;
import com.zhw.util.PageBean;
@WebServlet("/book/search")
public class BookServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BookDao bookDao = new BookDao();
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
Book book = new Book();
book.setBname(req.getParameter("bname"));
try {
List<Book> list = bookDao.list(book , pageBean);
req.setAttribute("books", list);
req.setAttribute("pageBean", pageBean);
req.getRequestDispatcher("/bookList.jsp").forward(req, resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

边栏推荐
- 华为面试题: 高矮个子排队
- Tp6 framework integrates JWT for token authentication
- Summary of knowledge points about eigenvalues and eigenvectors of matrices in Chapter 5 of Linear Algebra (Jeff's self perception)
- LeetCode 3. 无重复字符的最长子串
- Raii memory management
- RAII内存管理
- How to set password complexity and timeout exit function in Oracle
- How to use vant to realize data paging and drop-down loading
- 原生JS怎么生成九宫格
- 2022 the action of protecting the net is imminent. Things about protecting the net
猜你喜欢

Uniapp address translation latitude and longitude

The MariaDB database was found 12 hours late

How to use vant to realize data paging and drop-down loading

Summary of knowledge points about eigenvalues and eigenvectors of matrices in Chapter 5 of Linear Algebra (Jeff's self perception)

【微信小程序】条件渲染 列表渲染 原来这样用?

What is the difference between a layer 3 switch and a layer 2 switch

Intel hex, Motorola S-Record format detailed analysis

浅谈IDEA的优化和使用

Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake

怎么使用Vant实现数据分页和下拉加载
随机推荐
How can redis+aop customize annotations to achieve flow restriction
快速排序、聚簇索引、寻找数据中第k大的值
SQLite use
Azure developer news flash list of events of developers in June
华为面试题: 分糖果
可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写
Comparable和Comparator的区别
Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake
Interrupt operation: abortcontroller learning notes
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
Customize the buttons of jvxetable and the usage of $set under notes
How to use redis to realize the like function
Summary of PHP test sites encountered in CTF questions (I)
List of development tools
Xunwei NXP itop-imx6 development platform
Cmake tutorial series -04- compiling related functions
[Postgres] Postgres database migration
2022 underground coal mine electrical test and underground coal mine electrical simulation test
JMeter obtains cookies across thread groups or JMeter thread groups share cookies
Cmake tutorial series-03-dependency management