当前位置:网站首页>General paging - front desk
General paging - front desk
2022-07-28 23:35:00 【I love coriander TVT】
Catalog
One 、PageBean Paging encapsulation class
Last time I shared with you the background of general paging , Today, let's share with you the front desk
One 、PageBean Paging encapsulation class
First of all, we have to clarify how we need to implement front-end paging , Here are my ideas and methods for implementing front-end paging
1. You need to add a variable to save the last request address
2. You need to get the conditions of the first query
// You need to add a variable to save the last request address
private String Url ;
// You need to add variables to save the last query criteria
private Map<String, String[]> parameterMap = new HashMap<String, String[]>();Ideas : Because we may get the value of multiple conditions , So what is returned is an array of strings , Simultaneous use Map Collection for storage
3. You need to add methods , Get the page number of the largest page : If you don't understand, you can read the notes
public int MaxPage() {
// Divide the total data by the total number of rows If it is equal to 0 It means that you can get this value if you can divide it completely ,
// If it can't be divided completely, add... To this value 1
return this.total%this.rows==0?
this.total/this.rows:
this.total/this.rows+1;
}4. You need to add methods , Gets the page number of the next page : If you don't understand, you can read the notes
public int nextPage() {
// If the current number of pages is less than the maximum number of pages, it indicates that there is a value for the next page , Just +1 Represents the next page ,
// If it is larger than the maximum page, it will stay on the current page
return this.page<this.MaxPage()?this.page+1:this.page;
}5. You need to add methods , Get the page number of the previous page : If you don't understand, you can read the notes
public int previousPage() {
// If the current number of pages is greater than 1 Note that there is also the value of the previous page , Just -1 Represents the previous page
// If it is less than 1 Just stay on the current page
return this.page>1?this.page-1:this.page;
}At the same time, we define a value saving method in the paged encapsulation class , Five variables or methods required for paging ( Page number page、rows Page size 、 Pagination or not pagination、 Address Url、 Query criteria ParameterMap) For storage , And then again Servlet Just call this method directly in
public void setRequest(HttpServletRequest request) {
this.setPage(request.getParameter("page"));
this.setRows(request.getParameter("rows"));
this.setPagination(request.getParameter("pagination"));
this.setUrl(request.getRequestURL().toString());
this.setParameterMap(request.getParameterMap());
}
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));
}
}
private void setPage(String page) {
if(StringUtils.isNotBlank(page))
//set Automatic generation method
this.setPage(Integer.valueOf(page));
}Two 、PageTag Pagination label
We use the customization we shared before jsp The knowledge of tags comes from defining a Page label ,PageTag Inherit BodyTagSupport class , use stringbuffer Medium append Method to achieve the effect of page display . Finally, we just have to jsp Page references PageTag A label will do
package com.zjy.tag;
import java.io.IOException;
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.zjy.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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb = new StringBuffer();
// Hidden form Forms , To save the last query criteria
sb.append("<form action='"+pageBean.getUrl()+"' id='pageBeanForm' method='post'>");
sb.append(" <input type='hidden' name='page' value=''>");
Map<String, String[]> parameterMap = pageBean.getParameterMap();
if(parameterMap != null && parameterMap.size() > 0) {
Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
String key = entry.getKey();// name/likes/page/rows
String[] values = entry.getValue();
if(!"page".equals(key)) {
for (String value : values) {
sb.append(" <input type='hidden' name='"+key+"' value='"+value+"'>");
}
}
}
}
sb.append("</form>");
// Pagination bar
sb.append("<ul class='pagination justify-content-center'>");
sb.append(" <li class='page-item "+(pageBean.getPage() == 1 ? "disabled" : "")+"'><a class='page-link'");
sb.append(" href='javascript:gotoPage(1)'> home page </a></li>");
sb.append(" <li class='page-item "+(pageBean.getPage() == 1 ? "disabled" : "")+"'><a class='page-link'");
sb.append(" href='javascript:gotoPage("+pageBean.previousPage()+")'><</a></li>");
sb.append(" <li class='page-item active'><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>");
// Pagination js Code
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.toString();
}
}
Remember to define the tag in the tag library !
3、 ... and 、Jsp Interface
We are Servlet To call our paging method and use request.setAttribute Save value and forward to jsp Interface Servlet The code in :
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("BookList.jsp").forward(request, response);Interface reference :
<z:Page pageBean="${PageBean}"></z:Page>summary : The first query of front-end paging is the key and difficult point , We need to get the keywords of the query criteria, as well as the initial page number and total row number , Pagination or not , For the second and third pagination, we just need to change the page number , Other conditions need not be changed . We put the implementation effects of methods and interfaces into classes , Finally, just call , Very convenient !
边栏推荐
- High quality subroutine 1
- 一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
- Cnpm installation steps
- Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
- Introduction to original code, inverse code and complement code
- JS small method
- Win11快捷复制粘贴不能用怎么办?Win11快捷复制粘贴不能用
- MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
- Zero vision technology completed the pre-A round of financing and promoted the domestic replacement of intelligent driving platform software
- trivy【3】自定义扫描策略
猜你喜欢

Advanced C language: pointer (3)

Rhce第二天

Meet the outbreak period with the integration of transportation and parking, rush for mass production or build a technical moat?

Achieve high throughput through Wi Fi 7 - insight into the next generation of Wi Fi physical layer

深开鸿:万物智联的大江上,升起一轮开源鸿蒙月

Arduino框架下STM32F103C系列单片机引脚映射关系

Applet, JS, transfer object jump transfer parameter problem

mongodb索引添加、查看、导出、删除

「行泊一体」放量,福瑞泰克高性能域控制器领跑新赛道

事件抽取文献整理(2018)
随机推荐
当初的“你“为什么做测试/开发程序员?自己存在的价值......
Terminal output G_ Debug() information
General principles of software quality
通过Wi-Fi 7实现极高吞吐量——洞察下一代Wi-Fi物理层
How to open a profitable gym? I tell you from one year's experience that don't fall in love
被忽视的智能电视小程序领域
Combination of smart TV and applet
MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
All aspect visual monitoring of istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
Trivy [3] custom scanning strategy
字节8年女测试总监工作感悟—写给想转行或即将进入测试行业的女生们...
金仓数据库KingbaseES 客户端编程接口指南 - ODBC (2. 概述)
酒店预订系统数据库房间库存设计思路
Typescript类的使用
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
「行泊一体」放量,福瑞泰克高性能域控制器领跑新赛道
6 个超级良心的开源教程!
可视化全链路日志追踪
Advanced C language: pointer (3)
Typescript防止基类被实例化