定義最簡(jiǎn)單的標(biāo)簽
自定義標(biāo)簽采用Default Adapter模式(缺省適配模式)
配置文件
JSP頁(yè)面
TagSupport的流程圖
EVAL_BODY_INCLUDE 處理標(biāo)記內(nèi)容,并把這些內(nèi)容寫到輸出流中doStartTag()
SKIP_BODY 不處理標(biāo)記內(nèi)容doStartTag(),doAfterBody()
EVAL_BODY_AGAIN 又重頭處理doAfterBody()
EVAL_PAGE 繼續(xù)執(zhí)行JSP里面的代碼 doEndTag()
SKIP_PAGE 不繼續(xù)執(zhí)行JSP里面的代碼 doEndTag()
自定義標(biāo)簽采用Default Adapter模式(缺省適配模式)
//最簡(jiǎn)單的標(biāo)簽 public class LangHuaTag extends TagSupport { private long startTime; private long endTime; public int doStartTag() throws JspException { startTime = System.currentTimeMillis(); //表示定制標(biāo)記里面有所包括的JSP頁(yè)面 return TagSupport.EVAL_BODY_INCLUDE; } public int doEndTag() throws JspException { endTime = System.currentTimeMillis(); long elapsed = endTime - startTime; try { JspWriter out = pageContext.getOut(); out.println("runtime is "+ elapsed); } catch (IOException e) { e.printStackTrace(); } //表示JSP頁(yè)面繼續(xù)運(yùn)行 return TagSupport.EVAL_PAGE; } } //代屬性的標(biāo)簽 public class DateTag extends TagSupport { private String pattern = "yyyy-MM-dd hh:mm:ss"; private Date date; //必須要有Set方法,因?yàn)槭菍傩钥梢栽O(shè)值 public void setPattern(String pattern) { this.pattern = pattern; } public void setDate(Date date) { this.date = date; } public int doEndTag() throws JspException { SimpleDateFormat sdf = new SimpleDateFormat(pattern); //如果沒(méi)有就是當(dāng)前時(shí)間 if(date==null){ date = new Date(); } JspWriter out = pageContext.getOut(); try { out.print(sdf.format(date)); } catch (IOException e) { e.printStackTrace(); } return TagSupport.EVAL_PAGE; } } /** * 循環(huán)輸出 * @author Administrator * */ public class LoopTag extends TagSupport { private int times =0; //Set方法設(shè)值 public void setTimes(int times) { this.times = times; } public int doStartTag() throws JspException { //表示定制標(biāo)記里面有所包括的JSP頁(yè)面 return TagSupport.EVAL_BODY_INCLUDE; } public int doAfterBody() throws JspException { if(times>0){ times--; //表示雙從標(biāo)簽開始輸入 return TagSupport.EVAL_BODY_AGAIN; } //表示結(jié)束,忽略標(biāo)簽內(nèi)部的內(nèi)容 return TagSupport.SKIP_BODY; } }
配置文件
<?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"> <tlib-version>1.0</tlib-version> <short-name>util</short-name> <uri>http://langhua.com/taglib/util</uri> <tag> <name>timer</name> <tag-class>com.langhua.tagsupport.LangHuaTag</tag-class> <body-content>JSP</body-content> <!-- JSP,empty表示能能包函內(nèi)容的,scriptless,tagdependent --> </tag> <tag> <name>date</name> <tag-class>com.langhua.tagsupport.DateTag</tag-class> <body-content>empty</body-content> <!-- JSP,empty表示不能包函內(nèi)容的,scriptless,tagdependent --> <attribute> <!-- 標(biāo)簽名 --> <name>time</name> <!-- 是否為可選屬性 --> <required>false</required> <!-- 是否接受JSP表達(dá)示計(jì)算結(jié)果 --> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pattern</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>loop</name> <tag-class>com.langhua.tagsupport.LoopTag</tag-class> <body-content>JSP</body-content> <!-- JSP,empty表示不能包函內(nèi)容的,scriptless,tagdependent --> <attribute> <!-- 標(biāo)簽名 --> <name>times</name> <!-- 是否為可選屬性 --> <required>true</required> <!-- 是否接受JSP表達(dá)示計(jì)算結(jié)果 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
JSP頁(yè)面
<%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%> <util:timer></util:timer> <util:loop times="3"> <util:date pattern="yyyy-MM-dd" /><br/> </util:loop>
TagSupport的流程圖

EVAL_BODY_INCLUDE 處理標(biāo)記內(nèi)容,并把這些內(nèi)容寫到輸出流中doStartTag()
SKIP_BODY 不處理標(biāo)記內(nèi)容doStartTag(),doAfterBody()
EVAL_BODY_AGAIN 又重頭處理doAfterBody()
EVAL_PAGE 繼續(xù)執(zhí)行JSP里面的代碼 doEndTag()
SKIP_PAGE 不繼續(xù)執(zhí)行JSP里面的代碼 doEndTag()
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
