生活随笔
收集整理的這篇文章主要介紹了
实例应用 自定义页面taglib标签
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
關(guān)于繼承TagSupport 與 BodyTagSupport 的區(qū)別說(shuō)明
?*?<code>TagSupport</code>與 <code>BodyTagSupport</code> 的區(qū)別主要是標(biāo)簽處理類(lèi)是否需要與標(biāo)簽體交互。
?*?如果不需要交互的就用 <code>TagSupport</code> ,如果需要交互就用 <code>BodyTagSupport</code> 。
?*?交互就是標(biāo)簽處理類(lèi)是否要讀取標(biāo)簽體的內(nèi)容和改變標(biāo)簽體返回的內(nèi)容。
?*?用 <code>TagSupport</code> 實(shí)現(xiàn)的標(biāo)簽,都可以用 <code>BodyTagSupport</code> 來(lái)實(shí)現(xiàn),因?yàn)?/span><code>BodyTagSupport</code> 繼承了 <code>TagSupport</code> 。
?
實(shí)例應(yīng)用: 創(chuàng)建頁(yè)內(nèi)廣告標(biāo)簽
步驟一 : 創(chuàng)建標(biāo)簽對(duì)應(yīng)的 tld 文件 , 實(shí)例中文件名稱(chēng)為 plugin.tld, 將文件放置在 src 目錄下
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE taglibPUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN""http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
< taglib > < tlib-version > 1.0</ tlib-version > < jsp-version > 1.2</ jsp-version > <!-- follow config used in page include short-name value for prefix and uri value for uri --> < short-name > pudp</ short-name > < uri > http://org.pudp.com/webutil/advtag</ uri > < tag > < name > adv</ name > < tag-class > org.dennisit.util.tag.AdvTag</ tag-class > < body-content > empty</ body-content > < description > AdvTag for Page Content</ description > < attribute > < name > type</ name > < required > true</ required > < rtexprvalue > true</ rtexprvalue > < type > java.lang.String</ type > < description > resource type, value accept [media|image]</ description > </ attribute > < attribute > < name > src</ name > < required > true</ required > < rtexprvalue > true</ rtexprvalue > < type > java.lang.String</ type > < description > resource src, used for resource founding</ description > </ attribute > < attribute > < name > width</ name > < required > true</ required > < rtexprvalue > true</ rtexprvalue > < type > java.lang.String</ type > < description > width size for resource</ description > </ attribute > < attribute > < name > height</ name > < required > true</ required > < rtexprvalue > true</ rtexprvalue > < type > java.lang.String</ type > < description > height size for resource</ description > </ attribute > < attribute > < name > title</ name > < required > false</ required > < rtexprvalue > true</ rtexprvalue > < type > java.lang.String</ type > < description > title info show from mouse over on tag resource</ description > </ attribute > < attribute > < name > link</ name > < required > true</ required > < rtexprvalue > true</ rtexprvalue > < type > java.lang.String</ type > < description > target link for mouse click</ description > </ attribute > < attribute > < name > target</ name > < required > false</ required > < rtexprvalue > true</ rtexprvalue > < type > java.lang.String</ type > < description > resource open way, accept value [_blank,_self,_parent,_top], default value is _selft</ description > </ attribute > </ tag > </ taglib > ?
步驟二: 編寫(xiě)標(biāo)簽實(shí)現(xiàn)類(lèi)
package org.dennisit.util.tag; import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport; /** * 類(lèi)說(shuō)明:** <code>TagSupport</code>與<code>BodyTagSupport</code>的區(qū)別主要是標(biāo)簽處理類(lèi)是否需要與標(biāo)簽體交互。* * 如果不需要交互的就用<code>TagSupport</code>,如果需要交互就用<code>BodyTagSupport</code>。* * 交互就是標(biāo)簽處理類(lèi)是否要讀取標(biāo)簽體的內(nèi)容和改變標(biāo)簽體返回的內(nèi)容。* * 用<code>TagSupport</code>實(shí)現(xiàn)的標(biāo)簽,都可以用<code>BodyTagSupport</code>來(lái)實(shí)現(xiàn),因?yàn)?lt;code>BodyTagSupport</code>繼承了<code>TagSupport</code>。* * @author <a href='mailto:dennisit@163.com'>Cn.蘇若年(En.dennisit)</a> Copy Right since 2013-9-22 ** org.dennisit.util.tag.AdvTag.java* */ public class AdvTag extends TagSupport{ /** * */ private static final long serialVersionUID = 7474617660272039786L; private String type; // 資源類(lèi)型 private String src; // 資源路徑 private String width; // 資源寬度 private String height; // 資源高度 private String title; // 資源顯示標(biāo)簽 private String link; // 資源跳轉(zhuǎn)路徑 /** 資源打開(kāi)的方式 */ private String target = AdvTag.TARGET_SELF; // 設(shè)定資源的類(lèi)型 private static final String TYPE_MEDIA = "media"; private static final String TYPE_IMAGE = "image"; // 設(shè)定跳轉(zhuǎn)路徑 /** _blank 瀏覽器總在一個(gè)新打開(kāi) */ private static final String TARGET_BLANK = "_blank"; /** _self 這個(gè)目標(biāo)的值對(duì)所有沒(méi)有指定目標(biāo)的 標(biāo)簽是默認(rèn)目標(biāo) */ private static final String TARGET_SELF = "_self"; /** _parent 這個(gè)目標(biāo)使得文檔載入父窗口或者包含來(lái)超鏈接引用的框架的框架集。如果這個(gè)引用是在窗口或者在頂級(jí)框架中,那么它與目標(biāo) _self 等效。 */ private static final String TARGET_PARENT = "_parent"; /** _top 這個(gè)目標(biāo)使得文檔載入包含這個(gè)超鏈接的窗口,用 _top 目標(biāo)將會(huì)清除所有被包含的框架并將文檔載入整個(gè)瀏覽器窗口。 */ private static final String TARGET_TOP = "_top"; public AdvTag(){ this .setTitle(null ); this .setTarget(AdvTag.TARGET_SELF); this .setLink(link);}@Override public int doStartTag() throws JspException { // SKIP_BODY 表示不用處理標(biāo)簽體,直接調(diào)用doEndTag()方法。 return SKIP_BODY; /* 其它相關(guān)參數(shù)SKIP_PAGE 忽略標(biāo)簽后面的JSP內(nèi)容。EVAL_PAGE 處理標(biāo)簽后,繼續(xù)處理JSP后面的內(nèi)容。EVAL_BODY_BUFFERED 表示需要處理標(biāo)簽體。EVAL_BODY_INCLUDE 表示需要處理標(biāo)簽體,但繞過(guò)setBodyContent()和doInitBody()方法EVAL_BODY_AGAIN 對(duì)標(biāo)簽體循環(huán)處理。 */ }@Override public int doEndTag() throws JspException {StringBuilder ret =new StringBuilder(); // 如果是圖片資源廣告 if (this .type.equals(AdvTag.TYPE_IMAGE)) {ret.append( "<a href='").append(this .link).append("' target='"+this .target+"'>");ret.append( "<img src='").append(this .src); if (this .getTitle().trim().length()>0){ret.append( "' alt='").append(this .title);}ret.append( "' border='0' width='").append(this .width).append("' height='").append(this .height).append("'/></a>");} // 如果是flash資源廣告 if (this .type.equals(AdvTag.TYPE_MEDIA)) {ret.append( "<a href='").append(this .link); if (this .getTitle().trim().length()>0){ret.append( "' title='").append(this .title);}ret.append( "' target='"+this .target+"' style='cursor:pointer;'>");ret.append( "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' ");ret.append( "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' ");ret.append( " width='").append(this .width).append("' height='").append(this .height).append("'>");ret.append( "<param name='movie' value='").append(this .src).append("' />");ret.append( "<param name='quality' value='high' />");ret.append( "<embed src='").append(this .src).append("' quality='high' ");ret.append( "pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' ");ret.append( "width='").append(this .width).append("' height='").append(this .height).append("'></embed></object></a>");} try { this .pageContext.getOut().print(ret.toString());} catch (IOException e) {e.printStackTrace();} // 表示JSP頁(yè)面繼續(xù)運(yùn)行 return EVAL_PAGE;} public String getType() { return type;} public void setType(String type) { this .type = type;} public String getSrc() { return src;} public void setSrc(String src) { this .src = src;} public String getWidth() { return width;} public void setWidth(String width) { this .width = width;} public String getHeight() { return height;} public void setHeight(String height) { this .height = height;} public String getTitle() { return title;} public void setTitle(String title) { if (null !=title && !title.trim().equals("")){ this .title = title;} this .title = "";} public String getLink() { return link;} public void setLink(String link) { if (null !=link && link.trim().length()>0){ if (link.indexOf("http://")==-1){link = "http://" + link;}} this .link = link;} public String getTarget() { return target;} public void setTarget(String target) { if (target.equals(AdvTag.TARGET_BLANK)){ this .target = AdvTag.TARGET_BLANK;} if (target.equals(AdvTag.TARGET_PARENT)){ this .target = AdvTag.TARGET_PARENT;} if (target.equals(AdvTag.TARGET_SELF)){ this .target = AdvTag.TARGET_SELF;} if (target.equals(AdvTag.TARGET_TOP)){ this .target = AdvTag.TARGET_TOP;} // 其它的非法標(biāo)簽,都按照AdvTag.TARGET_SELF處理 this .target = AdvTag.TARGET_SELF;}} ?
步驟三: 在頁(yè)面內(nèi)使用標(biāo)簽
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<% @ taglib prefix = " pudp " uri = " http://org.pudp.com/webutil/advtag " %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html > < head > < title > 自定義標(biāo)簽</ title > < meta http-equiv ="pragma" content ="no-cache" > < meta http-equiv ="cache-control" content ="no-cache" > < meta http-equiv ="expires" content ="0" > < meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" > < meta http-equiv ="description" content ="This is my page" > </ head > < body > <!-- 使用自定義標(biāo)簽 放置圖片廣告 --> < pudp:adv type ="image" src ="adves/w470h40.png" height ="40px" width ="470px" link ="www.baidu.com" title ="百度廣告" /> <!-- 使用自定義標(biāo)簽放置flash廣告 --> < pudp:adv type ="media" src ="adves/dalib.swf" height ="80px" width ="400px;" link ="www.baidu.com" /> <!-- 使用html標(biāo)準(zhǔn)加載flash廣告的 --> < a href ="http://www.baidu.com" title ="百度廣告" > < OBJECT codeBase =http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 height =30 width =30 classid =clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 > < PARAM NAME ="movie" VALUE ="adves/dalib.swf" > < PARAM NAME ="quality" VALUE ="High" >< PARAM NAME ="wmode" VALUE ="transparent" > < param name ="menu" value ="false" > < param name =wmode value =opaque > < embed src ="adves/dalib.swf" quality ="High" pluginspage ="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type ="application/x-shockwave-flash" width ="400" height ="80" wmode ="transparent" menu ="false" ></ embed > </ OBJECT > </ a > </ body >
</ html > ?
引用方式說(shuō)明:
?
方式一:tld 文件防止在 src 目錄下 .jsp 頁(yè)面中引用
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<% @ taglib prefix = " pudp " uri = " http://org.pudp.com/webutil/advtag " %> 方式二: 將 tld 文件防止在 / WEB-INF/tld/目錄下, 在 jsp 頁(yè)面中可以使用下面方式引用
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<% @ taglib prefix = " pudp " uri = " /WEB-INF/tld/plugin.tld " %> 方式二中位置的的引用貌似也可以用方式一種那樣引用, 貌似 web 應(yīng)用啟動(dòng)后 , 只要 tld 在 web 容器可訪(fǎng)范圍內(nèi)都可以訪(fǎng)到 , 不知道對(duì)不對(duì) , 我測(cè)試時(shí)放置在 / WEB-INF/tld/目錄下, 使用方式一種的引用也是可以的 . 我放置在 WEB-INF目錄下,使用方式一的引入也是可以實(shí)現(xiàn)的 . 具體怎么找到的?應(yīng)該看一下源碼就明白了 .
代碼規(guī)整后結(jié)構(gòu)圖如下: 轉(zhuǎn)載請(qǐng)注明出處:[http://www.cnblogs.com/dennisit/p/3334276.html]
創(chuàng)作挑戰(zhàn)賽 新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔 為你收集整理的实例应用 自定义页面taglib标签 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。