A JSP paging component

Source: Internet
Author: User

This is simple and practical. I will share it with you here.

Package page;
Import java. util. List;
Import java. util. vector;

Public class pagination {

Private int sumpagenumber;
Private int sumitemnumber;
Private int pagenumber;
Private int showitemnumber;
Private string visitpageurl;
Private string pagehtml;
Public list interceptlistbystaritemnumber (list itemlist ){
Int startitem = (pageNumber-1) * showitemnumber;
List tpitemlist = new vector ();
If (itemlist = NULL ){
Return NULL;
}
Sumitemnumber = itemlist. Size ();
If (sumitemnumber % showitemnumber = 0 ){
Sumpagenumber = sumitemnumber/showitemnumber;
} Else {
Sumpagenumber = sumitemnumber/showitemnumber + 1;
}

For (INT I = 0; I <itemlist. Size (); I ++ ){
// Do not exceed the maximum number of truncated entries,
If (I) = (startitem + showitemnumber )){
Break;
}
// Add the object to the new list when the number of loops reaches the start.
If (I)> = startitem ){
Tpitemlist. Add (itemlist. Get (I ));
}
}

Return tpitemlist;
}

Public String buildhtml (string twidth ){
String tablewidth = twidth;
String visitfirstpageurl = "";
String visitnextpageurl = "";
String visitpreviusurl = "";
String visitendurl = "";
Visitpageurl = visitpageurl + "? Showitemnumber = "+ showitemnumber +" & pagenumber = ";
If (pagenumber = 1 ){
Visitfirstpageurl = "[homepage] & nbsp ;";
Visitpreviusurl = "[Previous Page] & nbsp ;";
} Else {
Visitfirstpageurl = "<a href = '" + visitpageurl + "1'> [homepage] </a> & nbsp ;";
Visitpreviusurl = "<a href = '" + visitpageurl + (pageNumber-1) + "'> [Previous Page] </a> & nbsp ;";
}
If (pagenumber = sumpagenumber ){
Visitnextpageurl = "[Next Page] & nbsp ;";
Visitendurl = "[end page] & nbsp ;";
} Else {
Visitnextpageurl = "<a href = '" + visitpageurl + (pagenumber + 1) + "'> [Next Page] </a> & nbsp ;";
Visitendurl = "<a href = '" + visitpageurl + sumpagenumber + "'> [last page] </a> ";
}

Stringbuffer sb = new stringbuffer ();
SB. append ("<Table width =" + tablewidth );
SB. append ("> <tr> <TD align = right> ");
SB. append ("current is page [" + pagenumber + "] & nbsp ;");
SB. append ("total [" + sumpagenumber + "] Page & nbsp;" + "total [" + sumitemnumber + "] & nbsp ;");
SB. append (visitfirstpageurl );
SB. append (visitpreviusurl );
SB. append (visitnextpageurl );
SB. append (visitendurl );
SB. append ("</TD> & nbsp; </tr> </table> ");
Return sb. tostring ();
}

Public int getpagenumber (){
Return pagenumber;
}

Public void setpagenumber (INT pagenumber ){
This. pagenumber = pagenumber;
}

Public int getshowitemnumber (){
Return showitemnumber;
}

Public void setshowitemnumber (INT showitemnumber ){
This. showitemnumber = showitemnumber;
}

Public int getsumitemnumber (){
Return sumitemnumber;
}

Public int getsumpagenumber (){
Return sumpagenumber;
}

Public String getvisitpageurl (){
Return visitpageurl;
}

Public void setvisitpageurl (string visitpageurl ){
This. visitpageurl = visitpageurl;
}

Public String getpagehtml (){
Return pagehtml;
}
}

JSP page call:

<% @ Page import = "Page. *" %>
<% @ Page import = "Java. util. *" %>

<% @ Page contenttype = "text/html; charset = GBK" %>

JSP paging component:
<%
Vector list = new vector ();
For (INT I = 0; I <100; I ++)
List. Add (I + "");
Pagination = NULL;
String pagenumber = request. getparameter ("pagenumber ");

Int showitemnumber = 5;
If (pagenumber = NULL ){
Pagenumber = "1 ";
}
String html = "";
If (list! = NULL & list. Size ()> 0 ){
Pagination = new pagination ();
Pagination. setpagenumber (integer. parseint (pagenumber ));
Pagination. setshowitemnumber (showitemnumber );
Pagination. setvisitpageurl ("allshetuan. jsp ");
List = (vector) pagination. interceptlistbystaritemnumber (list );
For (INT I = 0; I <list. Size (); I ++)
{
Out. Print ("<br>" + list. Get (I ));
}
Html = pagination. buildhtml ("600 ");
Out. Print (HTML );
}

%>

 

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.