当前位置:网站首页>通用分页(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();
}
}
}

边栏推荐
- What should academic presentation /ppt do?
- Azure developer news flash list of events of developers in June
- IDEA 远程调试 Remote JVM Debug
- JMeter obtains cookies across thread groups or JMeter thread groups share cookies
- Customize the buttons of jvxetable and the usage of $set under notes
- 重磅来袭--UE5的开源数字孪生解决方案
- Which is a good foreign exchange trading platform? Is it safe to have regulated funds?
- Quick sort, cluster index, find the k-largest value in the data
- Simulate activity startup mode in compose
- Global and Chinese market of mobile commerce solutions 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Recursion frog jumping steps problem

CMake教程系列-02-使用cmake代碼生成二進制

Implementation of Sanzi chess with C language

GTK interface programming (I): Environment Construction

Linear algebra Chapter 4 Summary of knowledge points of linear equations (Jeff's self perception)

Customize the buttons of jvxetable and the usage of $set under notes

How to set password complexity and timeout exit function in Oracle

HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification

【直播笔记0629】 并发编程二:锁

Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing
随机推荐
Lua Basics
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
Raii memory management
如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?
Quick sort, cluster index, find the k-largest value in the data
What is the concept of string in PHP
mysql 主从数据库同步失败的原因
(graph theory) connected component (template) + strongly connected component (template)
Unity timeline data binding
怎么使用Vant实现数据分页和下拉加载
Global and Chinese market of Kanban software 2022-2028: Research Report on technology, participants, trends, market size and share
What are the three paradigms of database
Global and Chinese markets for light cargo conveyors 2022-2028: Research Report on technology, participants, trends, market size and share
mysqldump原理
多卡服务器使用
IBM WebSphere channel connectivity setup and testing
2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...
Federal learning: dividing non IID samples by Dirichlet distribution
Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)
Summary of knowledge points about eigenvalues and eigenvectors of matrices in Chapter 5 of Linear Algebra (Jeff's self perception)