JSP generic paging code (super-managed) and jsp generic paging code

Source: Internet
Author: User

JSP generic paging code (super-managed) and jsp generic paging code

First, we will show you the paging effect. If you are still satisfied, please refer to the following code.


Parameters to be retained in the hyperlink

When a multi-condition query is used and the 2nd page is clicked, the 2nd page hyperlink has no conditions, so the conditions are lost, therefore, all links on the page must be retained!

We need to save the condition as a string to the url of PageBean! This task is handed over to Servlet!

Pagebean

Package cn. itcast. cstm. domain; import java. util. list; public class PageBean <T> {private int pc; // current page code // private int tp; // total page count total pageprivate int tr; // total number of records total recordprivate int ps; // number of records per page sizeprivate List <T> beanList; // private String url of the record on the current page; // 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 the total number of pages * @ return */public int getTp () {// calculate the total number of pages by the total number of records and the number of records per page. 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 () {return beanList;} public void setBeanList (List <T> beanList) {this. beanList = beanList ;}}

Jsp page

$ {Pb. pc} page/$ {pb in total. tp} page <a href = "$ {pb. url} & pc = 1 "> homepage </a> <c: if test =" $ {pb. pc> 1} "> <a href =" $ {pb. url} & pc =$ {pb. PC-1} "> previous page </a> </c: if> <% -- calculate begin, end -- %> <c: choose> <% -- if the total number of pages is less than 10, display all the pages! -- %> <C: when test = "$ {pb. tp <= 10} "> <c: set var =" begin "value =" 1 "/> <c: set var =" end "value =" $ {pb. tp} "/> </c: when> <c: otherwise> <% -- calculate begin and end -- %> <c: set var = "begin" value = "$ {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 = "10"/> </c: if ><%-- tail overflow -- %> <c: if test = "$ {end> pb. tp} "> <c: set var =" begin "value =" $ {pb. tp-9} "/> <c: set var =" end "value =" $ {pb. tp} "/> </c: if> </c: otherwise> </c: choose> <% -- cyclically traverse the page list -- %> <c: forEach var = "I" begin = "$ {begin}" end = "$ {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} & pc =$ {pb. tp} "> last page </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. C UstomerService; 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. if the pc parameter does not exist, pc = 1. If the pc parameter exists, convert it to the int type. */String value = request. getParameter ("pc"); if (value = null | value. trim (). isEmpty () {return 1;} Return Integer. parseInt (value);} // page servletpublic String query (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// System. out. println (getUrl (request);/** 0. encapsulate conditions in the Customer object. obtain pc 2. given ps 3. * use pc, ps, and condition object to call the service method to get PageBean 4. save PageBean to the request domain. * Forward to list. jsp * // obtain the query condition Customer criteria = CommonUtils. toBean (request. getParameterMap (), Cu Stomer. class);/** encoding of GET requests! */Criteria = encoding (criteria); int pc = getPc (request); // obtain pcint ps = 10; // The value of the given ps, page 10 records PageBean <Customer> pb = customerService. query (criteria, pc, ps); // obtain the url and save it to pb. setUrl (getUrl (request); request. setAttribute ("pb", pb); return "f:/list. jsp ";}/*** process four samples ** @ param criteria * @ return * @ throws UnsupportedEncodingException */private Customer encoding (Customer criteria) throws UnsupportedEncodingEx Ception {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"); criteria. setCname (cname);} if (gender! = Null &&! Gender. trim (). isEmpty () {gender = new String (gender. getBytes ("ISO-8859-1"), "UTF-8"); criteria. 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"); criteria. setEmail (email);} return criteria;}/*** intercept url/project name/Servlet Path? Parameter String ** @ param request * @ return */private String getUrl (HttpServletRequest request) {String contextPath = request. getContextPath (); // obtain the project name String servletPath = request. getServletPath (); // get servletPath, that is,/mermerservletstring queryString = request. getQueryString (); // obtain the parameter section after the question mark. // you can determine whether the parameter contains the pc parameter. If the parameter is included, You need to extract it. If (queryString. contains ("& pc =") {int index = queryString. lastIndexOf ("& pc ="); queryString = queryString. substring (0, index);} return contextPath + servletPath + "? "+ QueryString ;}}

The above content is a general JSP paging code (super-managed) shared by xiaobian. I hope it will help you.

Articles you may be interested in:
  • Pagination of JSP database operation data
  • Efficient jsp paging Query
  • A Practical JSP paging code
  • Paging code of jsp hibernate
  • A common jsp PageBean
  • Analysis on paging technology for jsp database reading
  • Sample Code displayed by JSP page
  • Introduction to jsp paging instances in json format (attached)
  • Implementation Code of jsp paging display
  • JSP custom pagination TAG

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.