当前位置:网站首页>General paging (2)
General paging (2)
2022-06-30 03:05:00 【An Li Jiu Ge】
Catalog
Two 、PageTag The implementation of the
Preface
Last time we shared the common paging (1), The content shared today is general paging (2). The content of this article is based on the previous article
One 、pageBean Perfection of
analysis :
Yes pagebean To initialize
initialization jsp The current page passed from the page
initialization jsp The page size passed from the page
initialization jsp Whether pages are paged after being transferred
Keep the last query request
Keep the last query criteria
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;// Page number
private int rows = 10;// Page size
private int total = 0;// Total number of records
private boolean pagination = true;// Pagination or not
private String url;
// Save last query criteria
private Map<String, String[]> parmeterMap = new HashMap<String, String[]>();
// Get the maximum page number
public int maxPage() {
return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1;
}
// Get the page number of the previous page
public int previousPage() {
return this.page > 1 ? this.page -1 : this.page;
}
// Gets the page number of the next page
public int nextPage() {
return this.page<this.maxPage() ? this.page +1 : this.page;
}
// initialization 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 Automatic generation method
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;
}
/**
* Get the subscript of the starting record
*
* @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 + "]";
}
}
Two 、PageTag The implementation of the
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)'> home page </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()+")'> Tail page </a></li>");
sb.append(" <li class='page-item go-input'><b> To the first </b><input class='page-link'");
sb.append(" type='text' id='skipPage' name='' /><b> page </b></li>");
sb.append(" <li class='page-item go'><a class='page-link'");
sb.append(" href='javascript:skipPage()'> determine </a></li>");
sb.append(" <li class='page-item'><b> common "+pagebean.getTotal()+" strip </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(' Please enter 1~"+pagebean.maxPage()+" The number of ');");
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();
}
}
}

边栏推荐
- 华为面试题: 高矮个子排队
- 行政路线编码 字母+数字的排序方式
- Auto. JS learning notes 15:ui interface basics of autojs Chapter 2
- Intel hex, Motorola S-Record format detailed analysis
- Azure developer news flash list of events of developers in June
- Tp6 framework integrates JWT for token authentication
- SQLite use
- List of development tools
- 怎样的外汇交易平台是有监管的,是安全的?
- 【直播笔记0629】 并发编程二:锁
猜你喜欢

OP-diode-限制摆幅

Distributed file system fastdfs

通用分页(2)

2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...

Three solutions to forced hibernation of corporate computers

Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0

约瑟夫环 数学解法

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

How to set password complexity and timeout exit function in Oracle

中断操作:AbortController学习笔记
随机推荐
2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...
2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...
Raki's notes on reading paper: neighborhood matching network for entity alignment
How to prevent duplicate submission under concurrent requests
Cmake tutorial series-01-minimum configuration example
通用分页(2)
Add a custom button to jvxetable
Raki's notes on reading paper: named entity recognition as dependency parsing
个人PC安装软件
DC/DC变换器轻载时三种工作模式的原理及优缺点
【微信小程序】条件渲染 列表渲染 原来这样用?
行政路线编码 字母+数字的排序方式
Shell counts all strings before the last occurrence of a string
oracle怎么设置密码复杂度及超时退出的功能
What is the concept of string in PHP
Summary of interview and Employment Questions
zabbix 触发器详解
PHP two-dimensional array randomly fetches a random or fixed number of one-dimensional arrays
GTK interface programming (I): Environment Construction
Heavy attack -- ue5's open source digital twin solution