對(duì)自定義標(biāo)簽添加一些屬性,可以使我們的標(biāo)簽功能更加靈活和復(fù)用。例如前一篇博客使用簡(jiǎn)單標(biāo)簽來(lái)對(duì)標(biāo)簽體內(nèi)容執(zhí)行一定的次數(shù),就無(wú)法在標(biāo)簽上規(guī)定要執(zhí)行的次數(shù),必須在標(biāo)簽處理器類中修改,很不方便,如果使用帶屬性的標(biāo)簽就能很好的解決這個(gè)問題。
要想使簡(jiǎn)單標(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)簽如下:
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:使用簡(jiǎn)單標(biāo)簽來(lái)控制標(biāo)簽體內(nèi)容執(zhí)行次數(shù)(帶屬性標(biāo)簽方式)
編寫標(biāo)簽處理器類:
package com.bjpowernode.simpletag; public class LoopTagBody extends SimpleTagSupport { private int count; //定義一個(gè)屬性,用來(lái)指定循環(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;ithis.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頁(yè)面的開頭導(dǎo)入taglib指令:
%@ taglib uri="simpletag" prefix="simple" %>
最后就能在JSP頁(yè)面的主體中使用剛才定義好的帶屬性的簡(jiǎn)單標(biāo)簽了,使用“count”屬性就能指定標(biāo)簽體循環(huán)的次數(shù):
simple:loopbody count="5"> 神樂! br> /simple:loopbody>
在瀏覽器中觀察:
通過(guò)上面的例子我們也可以看到,雖然“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容器無(wú)法將字符串轉(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頁(yè)面中導(dǎo)入taglib指令(此處略)后,在JSP頁(yè)面的主體中使用剛才定義的簡(jiǎn)單標(biāo)簽:
simple:showtime date="%=new Date() %>"/>
在瀏覽器中觀察:
因?yàn)樵贘SP頁(yè)面屬性上若以字符串,則因?yàn)樵跇?biāo)簽處理器類并非八大基本數(shù)據(jù)類型,因此只能使用JSP表達(dá)式或EL表達(dá)式將對(duì)象傳入,因此必須在TLD文件中將rtexprvalue>標(biāo)簽設(shè)置為“true”。
簡(jiǎn)單標(biāo)簽的應(yīng)用,包括無(wú)屬性的和帶屬性的標(biāo)簽如何使用都已經(jīng)學(xué)習(xí)完畢,內(nèi)容就這么多,剩下的就可以根據(jù)所學(xué)的進(jìn)行開發(fā)了。
例3:使用簡(jiǎn)單標(biāo)簽來(lái)防盜鏈
如果某個(gè)JSP頁(yè)面需要防止被別的網(wǎng)站盜鏈,可以在該JSP頁(yè)面的最開始部分使用一個(gè)簡(jiǎn)單標(biāo)簽,添加一些屬性如指定從哪過(guò)來(lái)的網(wǎng)站才可以瀏覽本頁(yè)面內(nèi)容,指定如果是非指定網(wǎng)址過(guò)來(lái)的鏈接應(yīng)該先讓請(qǐng)求跳到哪里去。
編寫標(biāo)簽處理器類:
package com.bjpowernode.simpletag; public class RefererTag extends SimpleTagSupport { private String site; //指定允許來(lái)訪請(qǐng)求的網(wǎng)址 private String location; //若非指定來(lái)訪請(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頁(yè)面中導(dǎo)入taglib指令(此處略)后,在JSP頁(yè)面的主體中使用剛才定義的簡(jiǎn)單標(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頁(yè)面,只有滿足請(qǐng)求的URL前綴為page屬性指定的網(wǎng)址才能訪問,如果是別的web中的超鏈接或者直接在瀏覽器中輸入該JSP的URL,都會(huì)被跳轉(zhuǎn)到location屬性指定的網(wǎng)頁(yè)。
例4:使用簡(jiǎn)單標(biāo)簽將標(biāo)簽體中的HTML過(guò)濾轉(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(""); break; case '"': result.append("quot;"); break; default: result.append(content[i]); } } return (result.toString()); } }
其中過(guò)濾方法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頁(yè)面中的主體部分中使用剛才自定義的簡(jiǎn)單標(biāo)簽:
simple:filterhtml> a href="www.baidu.com" rel="external nofollow" >百度/a> /simple:filterhtml>
瀏覽器中觀察:
標(biāo)簽:黃石 銅川 南京 樂山 大連 內(nèi)江 貸款邀約 廣州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP自定義標(biāo)簽-標(biāo)簽屬性_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理》,本文關(guān)鍵詞 JSP,自定義,標(biāo)簽,屬性,動(dòng)力,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。