JSP General high up paging code (super Works) _jsp programming

Source: Internet
Author: User

First show you the next page effect, if the parents are still very satisfied please refer to the following code.


To retain parameters in a hyperlink

When you use a multiple-condition query, and then click on page 2nd, this 2nd page hyperlink has no conditions, so will lose the conditions, so we need all the links on the page to retain the conditions!

We want to save the condition as a string into the Pagebean URL! This mission is given to servlet!.

Pagebean

 package cn.itcast.cstm.domain; import java.util.List; public class Pagebean<t> {p rivate int pc;//Current page number page code//private int tp;//Total pages Totals page private int tr;//Total records totals record private int ps;//per page record
Number page size Private list<t> beanlist;//The record of the current page private String url;//It is the condition after the URL!
Public String GetUrl () {return URL,} public void SetUrl (String url) {this.url = URL,} public int Getpc () {return PC;  public void Setpc (int pc) {this.pc = PC;}/** * Calculate total pages * @return/public int gettp () {//Total number of records and number of records per page to calculate gross pages int TP =
Tr/ps; Return tr%ps==0?
tp:tp+1; }//public void settp (int tp) {//THIS.TP = TP;//} public int gettr () {return TR;} public void settr (int tr) {this.
TR = TR; public int Getps () {return PS.} public void Setps (int ps) {this.ps = ps.} public list<t> Getbeanlist () {retur
n Beanlist; 
public void Setbeanlist (list<t> beanlist) {this.beanlist = Beanlist;}} 

JSP page

${PB.PC} page/Total ${PB.TP} page <a href= "${pb.url}&pc=1" > Home </a> <c:if test= "${pb.pc > 1}" > <a href= "${pb.url}&pc=${pb.pc-1}" > previous page </a> </c:if> <%--calculation begin, End--%> <c:choose> <%-- If the total page is less than 10 pages, then all the pages are displayed! --%> <c:when test= "${pb.tp <=}" > <c:set var= "Begin" value= "1"/> <c:set var= "End" value= "${PB.TP } "/> </c:when> <c:otherwise> <%--When the total number of pages >10, calculate begin and end--%> <c:set var=" Begin value= "by formula 
${pb.pc-5} "/> <c:set var=" End "value=" ${pb.pc+4} "/> <%--header overflow--%> <c:if test=" ${begin < 1} "> <c:set var= "Begin" value= "1"/> <c:set var= "End" value= "ten"/> </c:if> <%--tail overflow--%> <c:if te
st= "${end > PB.TP}" > <c:set var= "Begin" Value= "${pb.tp-9}"/> <c:set var= "End" value= "${pb.tp}"/> </c:if> </c:otherwise> </c:choose> <%--Loops traverse page number list--%> <c:foreach var= "i" begin= "${begin}" en D= "${end} "> <c:choose> <c:when test=" ${i eq pb.pc} "> [${i}] </c:when> <c:otherwise> <a href=" ${PB. URL}&pc=${i} ">[${i}]</a> </c:otherwise> </c:choose> </c:forEach> <c:if test=" ${ pb.pc < PB.TP} "> <a href=" ${pb.url}&pc=${pb.pc+1} "> next page </a> </c:if> <a href=" ${pb.url}& AMP;PC=${PB.TP} "> Last </a>

servlet

Package cn.itcast.cstm.web.servlet;
Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import java.util.List;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Cn.itcast.commons.CommonUtils;
Import Cn.itcast.cstm.domain.Customer;
Import Cn.itcast.cstm.domain.PageBean;
Import Cn.itcast.cstm.service.CustomerService;
Import Cn.itcast.servlet.BaseServlet; public class Customerservlet extends Baseservlet {private CustomerService customerservice = new CustomerService ();/** * Get PC * * @param request * @return/private int getpc (HttpServletRequest request) {/* 1. Get PC If PC parameter does not exist, description pc=1 if PC parameter
exists and needs to be converted to int type/String value = Request.getparameter ("PC");
if (value = null | | Value.trim (). IsEmpty ()) {return 1;} return Integer.parseint (value); //Paging servlet public String query (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//SysTem.out.println (GETURL (request)); * * 0. Encapsulates the condition to 1 of the customer object. Get PC 2.
Given PS 3. * Using PC and PS, as well as conditional objects, invoke the service method to get Pagebean 4.
Save the Pagebean to 5 in the request field.
* Forward to list.jsp///Get query conditions the Customer criteria = Commonutils.tobean (Request.getparametermap (), customer.class);
* * Processing GET request mode encoding problem!
* * Criteria = encoding (criteria); int pc = GETPC (Request)//get PC int PS = 10;//given PS value, page 10 line record pagebean<customer> PB = Customerservice.query (criteria
, PC, PS);
Get the URL and save it to the PB Pb.seturl (Request) (GETURL);
Request.setattribute ("PB", Pb);
return "f:/list.jsp"; /** * Processing Four * * @param criteria * @return * @throws unsupportedencodingexception/Private customer encoding (customer C
Riteria) throws Unsupportedencodingexception {String cname = criteria.getcname ();
String gender = Criteria.getgender ();
String cellphone = Criteria.getcellphone ();
String email = criteria.getemail (); if (CNAME!= null &&!cname.trim (). IsEmpty ()) {cname = new String (cname.getbytes ("iso-8859-1"), "Utf-8"); criteri A.setcname (CNAME); } if (gender!= null &&!gender.trim (). IsEmpty ()) {gender = new String (gender.getbytes ("iso-8859-1"), "Utf-8");
Riteria.setgender (gender); } if (cellphone!= null &&!cellphone.trim (). IsEmpty ()) {cellphone = new String (cellphone.getbytes ("Iso-8859-1")
, "Utf-8");
Criteria.setcellphone (cellphone); } if (email!= null &&!email.trim (). IsEmpty ()) {email = new String (email.getbytes ("iso-8859-1"), "Utf-8"); crite
Ria.setemail (email);
} return criteria; /** * Intercept URL/project name/servlet path? parameter String * * @param request * @return/private String GETURL (HttpServletRequest request) {St Ring ContextPath = Request.getcontextpath ()//Get project name String Servletpath = Request.getservletpath ();//get servletpath, i.e./
Customerservlet String querystring = request.getquerystring ()///whether the parameter part//judge parameter part contains the PC this parameter, if included, need to intercept, not this part. if (Querystring.contains ("&pc=")) {int index = Querystring.lastindexof ("&pc="); querystring =
Querystring.substring (0, index); Return ContextPath +Servletpath + "?" + querystring; }
}

Above is a small part of the JSP to share the general high page code (super Works), I hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.