国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP自定義標(biāo)簽-標(biāo)簽屬性_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

2024-09-05 00:23:14
字體:
供稿:網(wǎng)友

對(duì)自定義標(biāo)簽添加一些屬性,可以使我們的標(biāo)簽功能更加靈活和復(fù)用。例如前一篇博客使用簡單標(biāo)簽來對(duì)標(biāo)簽體內(nèi)容執(zhí)行一定的次數(shù),就無法在標(biāo)簽上規(guī)定要執(zhí)行的次數(shù),必須在標(biāo)簽處理器類中修改,很不方便,如果使用帶屬性的標(biāo)簽就能很好的解決這個(gè)問題。

  要想使簡單標(biāo)簽具有屬性,通常需要滿足以下兩個(gè)步驟:

  ① 在標(biāo)簽處理器類中定義屬性,同時(shí)為每個(gè)屬性生成setter方法;

  ② 在TLD文件中對(duì)于的<tag>標(biāo)簽下添加屬性的<attribute>標(biāo)簽,同時(shí)<attribute>標(biāo)簽下定義其從標(biāo)簽,其中<name>從標(biāo)簽是必須要有的。<attribute>標(biāo)簽所擁有的從標(biāo)簽如下:  

jsp,自定義標(biāo)簽,標(biāo)簽屬性

  name標(biāo)簽:用于指定標(biāo)簽中屬性的名稱。

  required標(biāo)簽:指定該屬性是否必須。

  rtexprvalue標(biāo)簽:指定該屬性是否支持運(yùn)行時(shí)表達(dá)式,如JSP表達(dá)式(<%=value  %>)和EL表達(dá)式( ${value} )。如果我們?cè)O(shè)定為“false”的話,那么該屬性只能支持字符串。 

例1:使用簡單標(biāo)簽來控制標(biāo)簽體內(nèi)容執(zhí)行次數(shù)(帶屬性標(biāo)簽方式)
編寫標(biāo)簽處理器類:

 package com.bjpowernode.simpletag; public class LoopTagBody extends SimpleTagSupport {   private int count; //定義一個(gè)屬性,用來指定循環(huán)次數(shù)   public void setCount(int count) {  //為該屬性設(shè)置setter方法     this.count = count;   }   @Override   public void doTag() throws JspException, IOException {       JspFragment fragment = this.getJspBody();     for(int i=0;i<this.count;i++) {  //使用屬性就可以指定循環(huán)次數(shù)       fragment.invoke(null);     }   }   }  

 在TLD文件中定義和描述標(biāo)簽處理器類,同時(shí)指定標(biāo)簽所在的uri:

<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"   version="2.0">   <description>A tag library exercising SimpleTag handlers.</description>   <tlib-version>1.0</tlib-version>   <short-name>SimpleTagLibrary</short-name> <uri>simpletag</uri>   <tag>     <name>loopbody</name>     <tag-class>com.bjpowernode.simpletag.LoopTagBody</tag-class>     <body-content>scriptless</body-content>     <attribute>       <name>count</name>       <required>true</required>       <rtexprvalue>true</rtexprvalue>     </attribute>   </tag> </taglib>

  在JSP頁面的開頭導(dǎo)入taglib指令:

  <%@ taglib uri="simpletag" prefix="simple" %>

最后就能在JSP頁面的主體中使用剛才定義好的帶屬性的簡單標(biāo)簽了,使用“count”屬性就能指定標(biāo)簽體循環(huán)的次數(shù):

 <simple:loopbody count="5">       神樂! <br>   </simple:loopbody>

在瀏覽器中觀察: 

jsp,自定義標(biāo)簽,標(biāo)簽屬性 

  通過上面的例子我們也可以看到,雖然“count”屬性在標(biāo)簽處理器LoopTagBody類中的類型為int整型,但是在標(biāo)簽上傳入的是字符串類型,這是因?yàn)镴SP容器支持將標(biāo)簽的屬性類型(字符串)轉(zhuǎn)換為八大基本數(shù)據(jù)類型。如果在標(biāo)簽處理器類中定義一個(gè)非八大基本數(shù)據(jù)類型的屬性,那么上面的以上面的方式必定要報(bào)錯(cuò),因?yàn)镴SP容器無法將字符串轉(zhuǎn)換為其它類型。除非在標(biāo)簽屬性中使用其它類型:

例2:

 package com.bjpowernode.simpletag; public class DateAttributeTag extends SimpleTagSupport {   private Date date;    public void setDate(Date date) {     this.date = date;   }   @Override   public void doTag() throws JspException, IOException {     this.getJspContext().getOut().write(date.toString());   } }

在TLD文件中描述(這里省略首尾,詳細(xì)內(nèi)容請(qǐng)看例1):

<tag> <name>showtime</name>    <tag-class>com.bjpowernode.simpletag.DateAttributeTag</tag-class>   <body-content>empty</body-content>   <attribute>       <name>date</name>       <required>true</required>       <rtexprvalue>true</rtexprvalue> </attribute> </tag>

注:這里<rtexprvalue>標(biāo)簽是必須要的。

在JSP頁面中導(dǎo)入taglib指令(此處略)后,在JSP頁面的主體中使用剛才定義的簡單標(biāo)簽:

  <simple:showtime date="<%=new Date() %>"/>

在瀏覽器中觀察:  

jsp,自定義標(biāo)簽,標(biāo)簽屬性

  因?yàn)樵贘SP頁面屬性上若以字符串,則因?yàn)樵跇?biāo)簽處理器類并非八大基本數(shù)據(jù)類型,因此只能使用JSP表達(dá)式或EL表達(dá)式將對(duì)象傳入,因此必須在TLD文件中將<rtexprvalue>標(biāo)簽設(shè)置為“true”。 

簡單標(biāo)簽的應(yīng)用,包括無屬性的和帶屬性的標(biāo)簽如何使用都已經(jīng)學(xué)習(xí)完畢,內(nèi)容就這么多,剩下的就可以根據(jù)所學(xué)的進(jìn)行開發(fā)了。

例3:使用簡單標(biāo)簽來防盜鏈

  如果某個(gè)JSP頁面需要防止被別的網(wǎng)站盜鏈,可以在該JSP頁面的最開始部分使用一個(gè)簡單標(biāo)簽,添加一些屬性如指定從哪過來的網(wǎng)站才可以瀏覽本頁面內(nèi)容,指定如果是非指定網(wǎng)址過來的鏈接應(yīng)該先讓請(qǐng)求跳到哪里去。

編寫標(biāo)簽處理器類:

 package com.bjpowernode.simpletag; public class RefererTag extends SimpleTagSupport {   private String site; //指定允許來訪請(qǐng)求的網(wǎng)址   private String location;    //若非指定來訪請(qǐng)求的網(wǎng)址應(yīng)該先跳轉(zhuǎn)到哪里去      public void setSite(String site) {     this.site = site;   }   public void setLocation(String location) {     this.location = location;   }   @Override   public void doTag() throws JspException, IOException {     PageContext pageContext = (PageContext) this.getJspContext();     HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();     HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();          String requestUrl = request.getHeader("referer");          if(requestUrl==null || !requestUrl.startsWith(site)) {       response.sendRedirect(request.getContextPath()+this.location);       throw new SkipPageException();     }   } }

在TLD文件中描述(這里省略首尾,詳細(xì)內(nèi)容請(qǐng)看例1):

 <tag>     <name>referer</name>     <tag-class>com.bjpowernode.simpletag.RefererTag</tag-class>     <body-content>empty</body-content>     <attribute>       <name>site</name>       <required>true</required>       <rtexprvalue>true</rtexprvalue>     </attribute>     <attribute>       <name>location</name>       <required>true</required>       <rtexprvalue>true</rtexprvalue>     </attribute> </tag>

在JSP頁面中導(dǎo)入taglib指令(此處略)后,在JSP頁面的主體中使用剛才定義的簡單標(biāo)簽:

 <simple:referer site="http://www.bjpowernode.com" location="/index.jsp" /> <!DOCTYPE HTML> <html>  <head>  <title>My JSP 'simpletagdemo.jsp' starting page</title>  </head>  。。。 </html>

  結(jié)果:若想訪問該JSP頁面,只有滿足請(qǐng)求的URL前綴為page屬性指定的網(wǎng)址才能訪問,如果是別的web中的超鏈接或者直接在瀏覽器中輸入該JSP的URL,都會(huì)被跳轉(zhuǎn)到location屬性指定的網(wǎng)頁。 

例4:使用簡單標(biāo)簽將標(biāo)簽體中的HTML過濾轉(zhuǎn)義

編寫標(biāo)簽處理器類:

 package com.bjpowernode.simpletag; public class HtmlFilterTag extends SimpleTagSupport {   @Override   public void doTag() throws JspException, IOException {     JspFragment fragment = this.getJspBody();     StringWriter writer = new StringWriter();     fragment.invoke(writer);     StringBuffer buffer = writer.getBuffer();     String content = filter(buffer.toString());     this.getJspContext().getOut().write(content);   }   public String filter(String message) {     if (message == null)       return (null);     char content[] = new char[message.length()];     message.getChars(0, message.length(), content, 0);     StringBuilder result = new StringBuilder(content.length + 50);     for (int i = 0; i < content.length; i++) {       switch (content[i]) {       case '<':         result.append("<");         break;       case '>':         result.append(">");         break;       case '&':         result.append("&");         break;       case '"':         result.append(""");         break;       default:         result.append(content[i]);       }     }     return (result.toString());   } }

  其中過濾方法filter方法可以在Tomcat中參考代碼(位置:【Tomcat】--->【webapps】--->【examples】--->【W(wǎng)EB-INF】--->【classes】--->【utils】--->“HTMLFilter.java”)。
在TLD文件中定義和描述標(biāo)簽:

 <tag>     <name>filterhtml</name>     <tag-class>com.bjpowernode.simpletag.HtmlFilterTag</tag-class>     <body-content>scriptless</body-content>  </tag>

在JSP頁面中的主體部分中使用剛才自定義的簡單標(biāo)簽:

 <simple:filterhtml>    <a href="www.baidu.com" rel="external nofollow" >百度</a>   </simple:filterhtml>

瀏覽器中觀察:

jsp,自定義標(biāo)簽,標(biāo)簽屬性


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JSP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清水河县| 沙坪坝区| 晋州市| 化州市| 玉门市| 定陶县| 久治县| 宁阳县| 莎车县| 江山市| 贡觉县| 紫云| 娱乐| 榆社县| 天镇县| 醴陵市| 繁昌县| 德江县| 卫辉市| 海晏县| 左权县| 瑞金市| 南皮县| 长沙县| 舞钢市| 肥西县| 乡宁县| 原阳县| 太谷县| 长武县| 乃东县| 新泰市| 崇礼县| 仁布县| 苏州市| 三台县| 灵武市| 开阳县| 安康市| 上高县| 凤凰县|