欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年07月25日 10:38:06   投稿:mrr  
對自定義標(biāo)簽添加一些屬性,可以使我們的標(biāo)簽功能更加靈活和復(fù)用。下面通過本文給大家分享JSP自定義標(biāo)簽-標(biāo)簽屬性的相關(guān)知識,感興趣的朋友一起看看吧

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

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

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

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

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

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

  rtexprvalue標(biāo)簽:指定該屬性是否支持運行時表達式,如JSP表達式(<%=value  %>)和EL表達式( ${value} )。如果我們設(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; //定義一個屬性,用來指定循環(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)簽處理器類,同時指定標(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>

在瀏覽器中觀察: 

 

  通過上面的例子我們也可以看到,雖然“count”屬性在標(biāo)簽處理器LoopTagBody類中的類型為int整型,但是在標(biāo)簽上傳入的是字符串類型,這是因為JSP容器支持將標(biāo)簽的屬性類型(字符串)轉(zhuǎn)換為八大基本數(shù)據(jù)類型。如果在標(biāo)簽處理器類中定義一個非八大基本數(shù)據(jù)類型的屬性,那么上面的以上面的方式必定要報錯,因為JSP容器無法將字符串轉(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文件中描述(這里省略首尾,詳細內(nèi)容請看例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)簽處理器類并非八大基本數(shù)據(jù)類型,因此只能使用JSP表達式或EL表達式將對象傳入,因此必須在TLD文件中將<rtexprvalue>標(biāo)簽設(shè)置為“true”。 

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

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

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

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

 package com.bjpowernode.simpletag;
 public class RefererTag extends SimpleTagSupport {
   private String site; //指定允許來訪請求的網(wǎng)址
   private String location;    //若非指定來訪請求的網(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文件中描述(這里省略首尾,詳細內(nèi)容請看例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頁面,只有滿足請求的URL前綴為page屬性指定的網(wǎng)址才能訪問,如果是別的web中的超鏈接或者直接在瀏覽器中輸入該JSP的URL,都會被跳轉(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("&lt;");
         break;
       case '>':
         result.append("&gt;");
         break;
       case '&':
         result.append("&amp;");
         break;
       case '"':
         result.append("&quot;");
         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>

瀏覽器中觀察:

相關(guān)文章

最新評論