当前位置:网站首页>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 !
边栏推荐
- Cnpm installation steps
- Huawei wireless device configuration uses WDS technology to deploy WLAN services
- Retrofit Usage Summary
- General principles of software quality
- CV语义分割模型小抄(2)
- mgr.exe病毒导致启动程序启动失败
- 金仓数据库KingbaseES 客户端编程接口指南 - ODBC特性支持约束
- String string
- What's special about this wireless router, which is popular in the whole network?
- 6 open source tutorials of super conscience!
猜你喜欢
使用这个,你发的消息就无法被监控了
Price for volume has encountered "six consecutive declines" in sales. Can Volvo, which is no longer safe, turn around?
行泊ADAS摄像头前装搭载同比增长54.15%,TOP10供应商领跑
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
CV目标检测模型小抄(2)
顶级“黑客”能厉害到什么地步?无信号也能上网,专家:高端操作!
事件抽取文献整理(2008-2017)
Inheritance in swift
JSP tag case
The functions and differences of display, visibility and overflow
随机推荐
With the "integration of driving and parking", freytek's high-performance domain controller leads the new track
金仓数据库KingbaseES客户端编程接口指南-ODBC(5. 开发过程)
二舅火了,全网刷屏,他凭什么能治好我的精神内耗?
Runloop principle (II)
Array array object
Few people can really play in the "aftermarket" of the whole house intelligent fire collection
Anr questions often asked in Android interviews
Rhce第二天
Sdwebimage source code comb 4 # introduce several usages of existing code
How to open a profitable gym? I tell you from one year's experience that don't fall in love
Typescript class method this pointer binding
Rouyi cloud platform - how to realize the launch and login functions of the project and how to create new modules
网络流量监控工具iftop
Kotlin JVM annotation
Byte 8 years' experience of female test Director - for girls who want to change careers or are about to enter the testing industry
22牛客多校day1 I - Chiitoitsu 概论dp
顶级“黑客”能厉害到什么地步?无信号也能上网,专家:高端操作!
Rhce第一天
Retrofit Usage Summary
Runloop, auto release pool, thread, GCD