JSP custom tab TAG whole process _jsp programming

Source: Internet
Author: User
Tags abstract prev tld stringbuffer

First we need to create a page.tld file in the web-inf/tld/directory

<?xml version= "1.0" encoding= "iso-8859-1"?> <taglib> <tlib-version>1.0</tlib-version> < Jsp-version>1.2</jsp-version> <short-name>page</short-name> <uri>http://www.kevin.com /taglibs/page</uri> <tag> <name>htmlPage</name> <tag-class> Com.lss.common.tag.pagetag</tag-class> <body-content>JSP</body-content> <description> HtmlPage tag</description> <attribute> <name>pageNo</name> <required>false</ required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pagesize </name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute > <attribute> <name>totalSum</name> <required>true</required> <rtexprvalue> true</rtexprvalue> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>showPage</name> <required>false</required> <rtexprvalue>true</rtexprvalue > </attribute> </tag> </taglib>

Package Com.lss.common.tag; 

Import java.io.IOException; 
Import javax.servlet.jsp.JspException; 
Import Javax.servlet.jsp.JspWriter; 

Import Javax.servlet.jsp.tagext.TagSupport; 
Import Com.lss.common.page.HtmlPage; 
Import Com.lss.common.page.Page; 

Import Com.lss.common.page.VariablePage; /** * Paging Label * * @description * @DATE 2012-3-11 PM 09:08:46/public class Pagetag extends TagSupport {private int 
PageNo; 
private int pageSize = 10; 
private int totalSum; 
private int showpage = 10; 

Private String URL; 
public int Getpageno () {return pageno; 
The public void Setpageno (int pageno) {This.pageno = PageNo; 
public int getpagesize () {return pageSize; 
The public void setpagesize (int pageSize) {this.pagesize = pageSize; 
public int gettotalsum () {return totalSum; 
The public void settotalsum (int totalSum) {this.totalsum = TotalSum; 
public int getshowpage () {return showpage; } public void Setshowpage (int showpage) {this.showpage = ShowPage; 
Public String GetUrl () {return URL; 
public void SetUrl (String url) {this.url = URL; @Override public int Doendtag () throws Jspexception {if (pageSize = = 0) {return tagsupport.skip_page;//does not show paging} 
else if (PageSize > TotalSum) {return tagsupport.skip_body;//does not display the paging} jspwriter out = Pagecontext.getout (); try {if (Url.indexof ("; jsessionid=") = = 1) {if (Url.indexof ("?") = = 1) {url = = '; jsessionid= ' + pagecontext.getses 
Sion (). GetId () + "? pageno="; 
else {URL = url.replacefirst ("\", "; jsessionid=" + pagecontext.getsession (). GetId () + "?"); 
if (Url.indexof ("pageno=") = = 1) {URL + "&pageno="; 
}} URL = Pagecontext.getservletcontext (). Getcontextpath () + URL; 
Variablepage variablepage = new Variablepage (PageNo, TotalSum, pageSize, URL); 
Variablepage.setshowpagenum (ShowPage); 
Page page = new HtmlPage (variablepage); Out.print (Page.getfirstno () + "" + page.getbackpagenum () + "" + page.pagination () + "" + page.getnextpagenum () +"" + Page.getlastno ()); 
catch (IOException ex) {ex.printstacktrace (); 
return Super.doendtag (); 
@Override public void release () {URL = null; 
PageNo = 0; 
TotalSum = 0; 
PageSize = 10; 
Super.release (); 
Private static final long serialversionuid = -2642907859641024483l; }

/** * HTML Pagination class * * @description * @DATE 2012-3-11 PM 10:33:14 * * public class HtmlPage extends Page {public Htmlpag 
E (Variablepage variablepage) {super (variablepage); 
public string pagination () {String printno = ""; If the page is paginated and the number of pages is less than equal to the number of pages to display if (Variablepage.totalpage > 1 && variablepage.totalpage <= 
Variablepage.showpagenum) {return displayall (); If pagination: greater than the number of pages displayed} else if (Variablepage.totalpage > 1 && variablepage.totalpage > Variablepage.showpagenu 
m) {if (Variablepage.pageno = 1) {//Current page equals first page return Fromfirstpageprint (); 
else if (Variablepage.pageno = = variablepage.totalpage) {//current page equals last page return Fromlastpageprint (); else {//If the current page: that is not the homepage nor the last if (variablepage.showpagenum% 2 = 0) {//You can split the number int print$no = variablepage.showpagenum/ 
2; 
if (Variablepage.pageno >= print$no) {int index$no = Variablepage.pageno-print$no; 
if (Variablepage.pageno + print$no >= variablepage.totalpage) {return fromlastpageprint ();else {if (index$no = 0) Index$no = 1; for (int i = Index$no I < (variablepage.showpagenum + index$no); i++) {if (i = = Variablepage.pageno) {//If is current page: Do not add a connection 
URL Printno + + (i + variablepage.split); 
else {Printno + = (Builda (variablepage, i) + variablepage.split); 
else {return fromfirstpageprint ()}}} 
} else {//printed page number is not even: int print$no = VARIABLEPAGE.SHOWPAGENUM/2 + 1; if (Variablepage.pageno >= print$no && Variablepage.pageno + print$no < variablepage.totalpage) {int index 
$No = Variablepage.pageno-print$no + 1; for (int i = Index$no i < Variablepage.showpagenum + Index$no; i++) {if (i = = Variablepage.pageno) {//If is current page: do not add connection u 
RL Printno + + (i + variablepage.split); 
else {Printno + = (Builda (variablepage, i) + variablepage.split); 
' Else if ' (Variablepage.pageno <= print$no) {//return from the first page fromfirstpageprint (); 
else {return fromlastpageprint (); 
}} return (PRINTNO); 
else {return "1"; }} PubLic String Getbackpagenum () {if (Variablepage.pageno <= 1) {return Buildspan ("Previous page", Variablepage); 
else {return Builda ("prev", (Variablepage.url + (variablepage.pageno-1))); } public String Getnextpagenum () {if (Variablepage.pageno >= variablepage.totalpage) {return Buildspan ("next page") 
Variablepage); 
else {return Builda ("Next page", Variablepage.url + (Variablepage.pageno + 1)); } public string Buildspan (string text, Variablepage variablepage) {return "<span style=\" color:gray;\ ">" + te 
XT + "</span>"; 
public string Builda (string text, string url) {return "<a href=\" "+ URL +" \ > "+ text +" </a> "; Public String Builda (variablepage variablepage, int num) {return ("<a href=\" "+ variablepage.url + num +" \ "> 
"+ num + </a>");

 } 
}
Public abstract class Page {protected Variablepage variablepage; 
Public Page (Variablepage variablepage) {this.variablepage = Variablepage; 
Calculatetotalpage (); 
public int Getstartindex () {return (Getvalidpagenum ()-1) * variablepage.pagesize; 
Public String Getbackpagenum () {if (Variablepage.pageno <= 1) {return Buildspan ("Previous page", Variablepage); 
else {return Builda ("prev", Variablepage.geturl () + (variablepage.pageno-1)); } public String Getnextpagenum () {if (Variablepage.pageno >= variablepage.totalpage) {return Buildspan ("next page") 
Variablepage); 
else {return Builda ("Next page", Variablepage.geturl () + (Variablepage.pageno + 1)); }/** * Calculate total pages */private void Calculatetotalpage () {if (variablepage.totalsum% variablepage.pagesize = = 0) {V 
Ariablepage.totalpage = variablepage.totalsum/variablepage.pagesize; 
else {variablepage.totalpage = variablepage.totalsum/variablepage.pagesize + 1; } if (Variablepage.totalpage < varIablepage.pageno) {Variablepage.pageno = Variablepage.totalpage; 
else if (Variablepage.pageno < 1) {Variablepage.pageno = 1; 
} protected String Displayall () {StringBuilder sbuilder = new StringBuilder (10); 
Sbuilder.append (Variablepage.split); for (int i = 1; I <= variablepage.totalpage i++) {if (i = = Variablepage.pageno) {sbuilder.append (i + variablepage). 
Split); 
else {sbuilder.append (Builda (variablepage, i) + variablepage.split); 
} return sbuilder.tostring (); 

/** * Abstracts out Pagination Method * * @return/public abstract String pagination (); /** * Implementation starts with the first page * * @return/protected final String Fromfirstpageprint () {StringBuffer buffer = new StringBuffer 
(100); for (int i = 1; I <= variablepage.showpagenum i++) {if (i = = Variablepage.pageno) {//If is current page: Do not add connection URL Buffer.append 
(i + variablepage.split); 
else {buffer.append (Builda (variablepage, i) + variablepage.split); 
} return buffer.tostring (); /** * Implementation starts printing from last page * * @return 
* * Protected final String Fromlastpageprint () {StringBuffer buffer = new StringBuffer (100); 
int startpage = variablepage.totalpage-(variablepage.showpagenum-1); for (int i = StartPage i <= variablepage.totalpage; i++) {if (i = = Variablepage.pageno) {//If is current page: Do not add a connection URL buffer. 
Append (i + variablepage.split); 
else {buffer.append (Builda (variablepage, i) + variablepage.split); 
} return buffer.tostring (); 
Public String Getfirstno () {if (Isexistspagination ()) {return Builda ("Home", Variablepage.url + 1); 
else {return Buildspan ("Home page", variablepage); }/** * To determine if there is a paging * * @return/private Boolean isexistspagination () {if (Variablepage.totalsum > 1 &AMP;&A mp 
Variablepage.totalpage > 1) {return true; 
return false; Public String Getlastno () {if (Isexistspagination ()) {return Builda ("Last", Variablepage.url + Variablepage.totalpag 
e); 
else {return Buildspan ("Last", variablepage); } protected int Getvalidpagenum () {if (Variablepage.totalpage < Variablepage.pageno) 
{return Variablepage.pageno = Variablepage.totalpage; 
else if (Variablepage.pageno < 1) {return Variablepage.pageno = 1; 
else {return variablepage.pageno; 
} public Variablepage Getpagecontant () {return this.variablepage; 

Public abstract String Buildspan (string text, Variablepage variablepage); 

Public abstract String Builda (string text, string URL); 

Public abstract String Builda (variablepage variablepage, int num); 
Public String printmodifypagesize (int pageSize) {StringBuilder builder=new StringBuilder (100); 
Builder.append ("Each page <input type= ' text ' id= ' pageSize ' style= '" width:20px ' maxlength= ' 2 ' name= ' pageSize ' "); 
if (pagesize>0) {builder.append ("value= '" +pagesize+ ""); 
} builder.append ("/> Strip"); 
return builder.tostring (); } 
}
/** * Paging Base variable * * @description * @DATE 2012-3-11 PM 10:34:00/public class Simplevariable {/** * Show page number/Protec 
Ted int showpagenum = 10; 
/** * Connection URL */protected String URL = null; 
/** * Current page number/protected int pageno = 1; 
/** * Total page number/protected int totalpage = 1; 
/** * Total number of strips/protected int totalSum = 0; 

/** * Each page shows the number of bars */protected int pageSize = 10; 
Public String GetUrl () {return URL; 
public void SetUrl (String url) {this.url = URL; 
public int gettotalpage () {return totalpage; 
The public void settotalpage (int totalpage) {this.totalpage = Totalpage; 
public int gettotalsum () {return totalSum; 
The public void settotalsum (int totalSum) {this.totalsum = TotalSum; 
public int getpagesize () {return pageSize; 
The public void setpagesize (int pageSize) {this.pagesize = pageSize; 
public int Getshowpagenum () {return showpagenum; 
The public void setshowpagenum (int showpagenum) {this.showpagenum = Showpagenum; } public INT Getpageno () {return pageno; 
The public void Setpageno (int pageno) {This.pageno = PageNo;  } 
}
/** * Paging 
variable * 
@description 
* @DATE 2012-3-11 pm 10:34:08/Public 
class Variablepage extends simplevariable { 
protected String split = ""; 
protected String style = "class= ' page"; 

Public variablepage (int pageno, int totalSum, int pageSize, String url) { 
pageno = PageNo < 1? 1:pageno; 
Super.pageno = PageNo; 
This.totalsum = TotalSum; 
This.url = URL; 
This.pagesize = pageSize; 
} 

Public variablepage (int pagenum, int totalSum, String url) { 
pagenum = Pagenum < 1? 1:pagenum; 
Super.pageno = Pagenum; 
This.totalsum = TotalSum; 
This.url = URL; 
} 

Public String Getsplit () {return 
split; 
} 

public void Setsplit (String split) { 
this.split = split; 
} 

}

#需要使用该分页标签的JSP use the following methods:

JSP Head Introduction

<% @taglib uri= "http://www.kevin.com/taglibs/page" prefix= "page"%>

Use the following label raw page HTML code

<page:htmlpage pageno= "${pageno}" url= "Your_url" totalsum= "${totalsum}" "Showpage=" pagesize= "/>"

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.