生活随笔
收集整理的這篇文章主要介紹了
实例应用 自定义页面taglib标签
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于繼承TagSupport 與 BodyTagSupport 的區別說明
?*?<code>TagSupport</code>與 <code>BodyTagSupport</code> 的區別主要是標簽處理類是否需要與標簽體交互。
?*?如果不需要交互的就用 <code>TagSupport</code> ,如果需要交互就用 <code>BodyTagSupport</code> 。
?*?交互就是標簽處理類是否要讀取標簽體的內容和改變標簽體返回的內容。
?*?用 <code>TagSupport</code> 實現的標簽,都可以用 <code>BodyTagSupport</code> 來實現,因為 <code>BodyTagSupport</code> 繼承了 <code>TagSupport</code> 。
?
實例應用: 創建頁內廣告標簽
步驟一 : 創建標簽對應的 tld 文件 , 實例中文件名稱為 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 > ?
步驟二: 編寫標簽實現類
package org.dennisit.util.tag; import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport; /** * 類說明:** <code>TagSupport</code>與<code>BodyTagSupport</code>的區別主要是標簽處理類是否需要與標簽體交互。* * 如果不需要交互的就用<code>TagSupport</code>,如果需要交互就用<code>BodyTagSupport</code>。* * 交互就是標簽處理類是否要讀取標簽體的內容和改變標簽體返回的內容。* * 用<code>TagSupport</code>實現的標簽,都可以用<code>BodyTagSupport</code>來實現,因為<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; // 資源類型 private String src; // 資源路徑 private String width; // 資源寬度 private String height; // 資源高度 private String title; // 資源顯示標簽 private String link; // 資源跳轉路徑 /** 資源打開的方式 */ private String target = AdvTag.TARGET_SELF; // 設定資源的類型 private static final String TYPE_MEDIA = "media"; private static final String TYPE_IMAGE = "image"; // 設定跳轉路徑 /** _blank 瀏覽器總在一個新打開 */ private static final String TARGET_BLANK = "_blank"; /** _self 這個目標的值對所有沒有指定目標的 標簽是默認目標 */ private static final String TARGET_SELF = "_self"; /** _parent 這個目標使得文檔載入父窗口或者包含來超鏈接引用的框架的框架集。如果這個引用是在窗口或者在頂級框架中,那么它與目標 _self 等效。 */ private static final String TARGET_PARENT = "_parent"; /** _top 這個目標使得文檔載入包含這個超鏈接的窗口,用 _top 目標將會清除所有被包含的框架并將文檔載入整個瀏覽器窗口。 */ 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 表示不用處理標簽體,直接調用doEndTag()方法。 return SKIP_BODY; /* 其它相關參數SKIP_PAGE 忽略標簽后面的JSP內容。EVAL_PAGE 處理標簽后,繼續處理JSP后面的內容。EVAL_BODY_BUFFERED 表示需要處理標簽體。EVAL_BODY_INCLUDE 表示需要處理標簽體,但繞過setBodyContent()和doInitBody()方法EVAL_BODY_AGAIN 對標簽體循環處理。 */ }@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頁面繼續運行 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;} // 其它的非法標簽,都按照AdvTag.TARGET_SELF處理 this .target = AdvTag.TARGET_SELF;}} ?
步驟三: 在頁面內使用標簽
<% @ 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 > 自定義標簽</ 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 > <!-- 使用自定義標簽 放置圖片廣告 --> < pudp:adv type ="image" src ="adves/w470h40.png" height ="40px" width ="470px" link ="www.baidu.com" title ="百度廣告" /> <!-- 使用自定義標簽放置flash廣告 --> < pudp:adv type ="media" src ="adves/dalib.swf" height ="80px" width ="400px;" link ="www.baidu.com" /> <!-- 使用html標準加載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 > ?
引用方式說明:
?
方式一:tld 文件防止在 src 目錄下 .jsp 頁面中引用
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<% @ taglib prefix = " pudp " uri = " http://org.pudp.com/webutil/advtag " %> 方式二: 將 tld 文件防止在 / WEB-INF/tld/目錄下, 在 jsp 頁面中可以使用下面方式引用
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
<% @ taglib prefix = " pudp " uri = " /WEB-INF/tld/plugin.tld " %> 方式二中位置的的引用貌似也可以用方式一種那樣引用, 貌似 web 應用啟動后 , 只要 tld 在 web 容器可訪范圍內都可以訪到 , 不知道對不對 , 我測試時放置在 / WEB-INF/tld/目錄下, 使用方式一種的引用也是可以的 . 我放置在 WEB-INF目錄下,使用方式一的引入也是可以實現的 . 具體怎么找到的?應該看一下源碼就明白了 .
代碼規整后結構圖如下: 轉載請注明出處:[http://www.cnblogs.com/dennisit/p/3334276.html]
創作挑戰賽 新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔 為你收集整理的实例应用 自定义页面taglib标签 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。