JSP page Paging Hyperlink code implementation __JS

Source: Internet
Author: User


When a large number of data is listed, the screen cannot be fully displayed, you must use pagination, SQL paging has been discussed in other articles, here is the implementation of JSP Division code.



Page incoming parameter P as page selection, no parameter default first page.






Calculate the basic parameters of the page division, currentpage for the current page, PageRows set the number of rows per page, in the acquisition of Totalpage is total pages, the actual situation according to the data of the decision, here tentatively 4000.






Paging Parameters


  Default current page number 0
  long currentpage = 0;
  If the incoming argument p is valid, fetch the incoming argument
  if (Request.getparameter ("P")!= null)
  {
    //Get the current page parameter
    String p = Request.getparameter ("P");
    Try
    {
      //Get number of pages of numeric type
      currentpage = long.valueof (P);
    }
    catch (Exception e)
    {
      currentpage = 0;
    }
  }

  The total number of records (for example, taken from the database)
  long = 4000;
  Set the number of rows per page
  final long pagerows =;
  List 9 pages with current page not centered
  final int clickpagelist = 9;
  Calculates the number of pages with PageRows as line number
  long PageCount = total% PageRows;
  If the remainder is greater than 0 it must be under the total page + 1
  if (PageCount > 0)
  {
    //Get the actual page number
    PageCount = total/pagerows + 1;
  }
  Else
  {
    PageCount = total/pagerows;
  }





Paging Implementation


// If the number of pages is valid
If (pageCount > 0)
{
  // Set two fixed links for easy operation
  If (currentPage > 0)
  {
    Out.print(String.format("<A href=\"page.jsp?p=%s\" target=_blank>Previous</A> ", currentPage - 1));
  }
  Else
  {
    Out.print("previous page");
  }
 
  If (currentPage < pageCount - 1)
  {
    Out.print(String.format("<A href=\"page.jsp?p=%s\" target=_blank> Next page</A> ", currentPage + 1));
  }
  Else
  {
    Out.print("Next");
  }
 
  Out.print("<a href=\"page.jsp\"> Page 1</a> ");
 
  String line = "";
  // List the link to the left of the current page
  For (long i = currentPage; i >= 0; i--)
  {
    // limit the number listed to the left
    If (i < (currentPage - clickPageList))
    {
      Break;
    }
    Line = String.format("<a href=\"page.jsp?p=%s\">%s</a> ", i, i + 1) + line;
  }
  Out.print(line);
  Line = "";
 
  // List the link to the right of the current page
  For (long i = currentPage + 1; i <= pageCount - 1; i++)
  {
    // limit and list the quantity
    If (i >= (currentPage + clickPageList))
    {
      Break;
    }
    Line = line + String.format("<a href=\"page.jsp?p=%s\">%s</a> ", i, i + 1);
  }
  Out.print(line);
  Line = "";
 
  Out.print(String.format("<a href=\"page.jsp?p=%s\">last page</a>", pageCount - 1));
} 




The page selection code is as follows:


The demo effect is as follows, the actual use can replace the text with the corresponding picture or CSS style, in order to achieve better results







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.