主頁 > 知識庫 > JSP自定義分頁標(biāo)簽TAG全過程

JSP自定義分頁標(biāo)簽TAG全過程

熱門標(biāo)簽:杭州ai語音電銷機(jī)器人功能 電銷機(jī)器人是有一些什么技術(shù) 四川保險智能外呼系統(tǒng)商家 杭州語音電銷機(jī)器人軟件 高德地圖標(biāo)注樣式 電銷機(jī)器人好賣么 地圖標(biāo)注線上教程 商洛電銷 北票市地圖標(biāo)注

首先我們需要在WEB-INF/tld/目錄下創(chuàng)建page.tld文件

?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; 

/** 
* 分頁處理標(biāo)簽 
* 
* @description 
* @DATE 2012-3-11下午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; 
} 

public void setPageNo(int pageNo) { 
this.pageNo = pageNo; 
} 

public int getPageSize() { 
return pageSize; 
} 

public void setPageSize(int pageSize) { 
this.pageSize = pageSize; 
} 

public int getTotalSum() { 
return totalSum; 
} 

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;// 不顯示分頁 
} else if (pageSize > totalSum) { 
return TagSupport.SKIP_BODY;// 不顯示分頁 
} 
JspWriter out = pageContext.getOut(); 
try { 
if (url.indexOf(";jsessionid=") == -1) { 
if (url.indexOf("?") == -1) { 
url += ";jsessionid=" + pageContext.getSession().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 分頁類 
* 
* @description 
* @DATE 2012-3-11下午10:33:14 
*/ 
public class HtmlPage extends Page { 
public HtmlPage(VariablePage variablePage) { 
super(variablePage); 
} 

public String pagination() { 
String printNo = ""; 
// 如果已分頁;并且頁數(shù)小于等于要顯示的頁數(shù) 
if (variablePage.totalPage > 1  variablePage.totalPage = variablePage.showPageNum) { 
return displayAll(); 
// 如果分頁數(shù):大于顯示的頁碼數(shù) 
} else if (variablePage.totalPage > 1  variablePage.totalPage > variablePage.showPageNum) { 
if (variablePage.pageNo == 1) {// 當(dāng)前頁等于第一頁 
return fromFirstPagePrint(); 
} else if (variablePage.pageNo == variablePage.totalPage) {// 當(dāng)前頁等于最后一頁 
return fromLastPagePrint(); 
} else {// 如果當(dāng)前頁:即不是首頁也不是尾頁 
if (variablePage.showPageNum % 2 == 0) {// 可以平分頁碼 
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) {// 如果是當(dāng)前頁:不添加連接URL 
printNo += (i + variablePage.split); 
} else { 
printNo += (buildA(variablePage, i) + variablePage.split); 
} 
} 
} 
} else { 
return fromFirstPagePrint(); 
} 
} else {// 打印頁數(shù)不是偶數(shù)時: 
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) {// 如果是當(dāng)前頁:不添加連接URL 
printNo += (i + variablePage.split); 
} else { 
printNo += (buildA(variablePage, i) + variablePage.split); 
} 
} 
} else if (variablePage.pageNo = print$No) {// 從第一頁開始 
return fromFirstPagePrint(); 
} else { 
return fromLastPagePrint(); 
} 
} 
} 
return (printNo); 
} else { 
return "1"; 
} 
} 

public String getBackpageNum() { 
if (variablePage.pageNo = 1) { 
return buildSpan("上一頁", variablePage); 
} else { 
return buildA("上一頁", (variablePage.url + (variablePage.pageNo - 1))); 
} 
} 

public String getNextpageNum() { 
if (variablePage.pageNo >= variablePage.totalPage) { 
return buildSpan("下一頁", variablePage); 
} else { 
return buildA("下一頁", variablePage.url + (variablePage.pageNo + 1)); 
} 
} 

public String buildSpan(String text, VariablePage variablePage) { 
return "span style=\"color:gray;\">" + text + "/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("上一頁", variablePage); 
} else { 
return buildA("上一頁", variablePage.getUrl() + (variablePage.pageNo - 1)); 
} 
} 

public String getNextpageNum() { 
if (variablePage.pageNo >= variablePage.totalPage) { 
return buildSpan("下一頁", variablePage); 
} else { 
return buildA("下一頁", variablePage.getUrl() + (variablePage.pageNo + 1)); 
} 
} 

/** 
* 計(jì)算總頁數(shù) 
*/ 
private void calculateTotalPage() { 
if (variablePage.totalSum % variablePage.pageSize == 0) { 
variablePage.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(); 
} 

/** 
* 抽象出分頁方法 
* 
* @return 
*/ 
public abstract String pagination(); 

/** 
* 實(shí)現(xiàn)從第一頁開始打印 
* 
* @return 
*/ 
protected final String fromFirstPagePrint() { 
StringBuffer buffer = new StringBuffer(100); 
for (int i = 1; i = variablePage.showPageNum; i++) { 
if (i == variablePage.pageNo) {// 如果是當(dāng)前頁:不添加連接URL 
buffer.append(i + variablePage.split); 
} else { 
buffer.append(buildA(variablePage, i) + variablePage.split); 
} 
} 
return buffer.toString(); 
} 

/** 
* 實(shí)現(xiàn)從最后一頁開始打印 
* 
* @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) {// 如果是當(dāng)前頁:不添加連接URL 
buffer.append(i + variablePage.split); 
} else { 
buffer.append(buildA(variablePage, i) + variablePage.split); 
} 
} 
return buffer.toString(); 
} 

public String getFirstNo() { 
if (isExistsPagination()) { 
return buildA("首頁", variablePage.url + 1); 
} else { 
return buildSpan("首頁", variablePage); 
} 
} 

/** 
* 判斷是否存在分頁 
* 
* @return 
*/ 
private boolean isExistsPagination() { 
if (variablePage.totalSum > 1  variablePage.totalPage > 1) { 
return true; 
} 
return false; 
} 

public String getLastNo() { 
if (isExistsPagination()) { 
return buildA("尾頁", variablePage.url + variablePage.totalPage); 
} else { 
return buildSpan("尾頁", 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("每頁input type='text' id='pageSize' style='width:20px' maxlength='2' name='pageSize'"); 
if(pageSize>0){ 
builder.append(" value='"+pageSize+"' "); 
} 
builder.append("/>條"); 
return builder.toString(); 
} 
}
/** 
* 分頁基礎(chǔ)變量 
* 
* @description 
* @DATE 2012-3-11下午10:34:00 
*/ 
public class SimpleVariable { 
/** 
* 顯示頁碼 
*/ 
protected int showPageNum = 10; 
/** 
* 連接URL 
*/ 
protected String url = null; 
/** 
* 當(dāng)前頁碼 
*/ 
protected int pageNo = 1; 
/** 
* 總頁碼 
*/ 
protected int totalPage = 1; 
/** 
* 總條數(shù) 
*/ 
protected int totalSum = 0; 
/** 
* 每頁顯示條數(shù) 
*/ 
protected int pageSize = 10; 

public String getUrl() { 
return url; 
} 

public void setUrl(String url) { 
this.url = url; 
} 

public int getTotalPage() { 
return totalPage; 
} 

public void setTotalPage(int totalPage) { 
this.totalPage = totalPage; 
} 

public int getTotalSum() { 
return totalSum; 
} 

public void setTotalSum(int totalSum) { 
this.totalSum = totalSum; 
} 

public int getPageSize() { 
return pageSize; 
} 

public void setPageSize(int pageSize) { 
this.pageSize = pageSize; 
} 

public int getShowPageNum() { 
return showPageNum; 
} 

public void setShowPageNum(int showPageNum) { 
this.showPageNum = showPageNum; 
} 

public int getPageNo() { 
return pageNo; 
} 

public void setPageNo(int pageNo) { 
this.pageNo = pageNo; 
} 
} 
/** 
* 分頁變量 
* 
* @description 
* @DATE 2012-3-11下午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; 
} 

}

#需要使用該分頁標(biāo)簽的JSP 使用方法如下:

JSP 頭部引入

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

使用以下標(biāo)簽生成分頁HTML代碼

page:htmlPage pageNo="${pageNo}" url="your_url" totalSum="${totalSum}" showPage="10" pageSize="12"/>

您可能感興趣的文章:
  • jsp分頁顯示的實(shí)現(xiàn)代碼
  • JSP分頁顯示的實(shí)例代碼
  • 一個實(shí)用的JSP分頁代碼
  • JSP實(shí)現(xiàn)的簡單分頁示例
  • jsp hibernate的分頁代碼
  • jsp+servlet+javabean實(shí)現(xiàn)數(shù)據(jù)分頁方法完整實(shí)例
  • JSP通用高大上分頁代碼(超管用)
  • jsp實(shí)現(xiàn)頁面分頁功能代碼
  • 一個通用的jsp分頁P(yáng)ageBean
  • JSP實(shí)現(xiàn)分頁效果

標(biāo)簽:云浮 西藏 宿州 紅河 貴州 青島 江西 丹東

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP自定義分頁標(biāo)簽TAG全過程》,本文關(guān)鍵詞  JSP,自定義,分頁,標(biāo)簽,TAG,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《JSP自定義分頁標(biāo)簽TAG全過程》相關(guān)的同類信息!
  • 本頁收集關(guān)于JSP自定義分頁標(biāo)簽TAG全過程的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章