定義最簡單的標簽
自定義標簽采用Default Adapter模式(缺省適配模式)
配置文件
JSP頁面
TagSupport的流程圖
EVAL_BODY_INCLUDE 處理標記內容,并把這些內容寫到輸出流中doStartTag()
SKIP_BODY 不處理標記內容doStartTag(),doAfterBody()
EVAL_BODY_AGAIN 又重頭處理doAfterBody()
EVAL_PAGE 繼續執行JSP里面的代碼 doEndTag()
SKIP_PAGE 不繼續執行JSP里面的代碼 doEndTag()
自定義標簽采用Default Adapter模式(缺省適配模式)
//最簡單的標簽 public class LangHuaTag extends TagSupport { private long startTime; private long endTime; public int doStartTag() throws JspException { startTime = System.currentTimeMillis(); //表示定制標記里面有所包括的JSP頁面 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頁面繼續運行 return TagSupport.EVAL_PAGE; } } //代屬性的標簽 public class DateTag extends TagSupport { private String pattern = "yyyy-MM-dd hh:mm:ss"; private Date date; //必須要有Set方法,因為是屬性可以設值 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); //如果沒有就是當前時間 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; } } /** * 循環輸出 * @author Administrator * */ public class LoopTag extends TagSupport { private int times =0; //Set方法設值 public void setTimes(int times) { this.times = times; } public int doStartTag() throws JspException { //表示定制標記里面有所包括的JSP頁面 return TagSupport.EVAL_BODY_INCLUDE; } public int doAfterBody() throws JspException { if(times>0){ times--; //表示雙從標簽開始輸入 return TagSupport.EVAL_BODY_AGAIN; } //表示結束,忽略標簽內部的內容 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表示能能包函內容的,scriptless,tagdependent --> </tag> <tag> <name>date</name> <tag-class>com.langhua.tagsupport.DateTag</tag-class> <body-content>empty</body-content> <!-- JSP,empty表示不能包函內容的,scriptless,tagdependent --> <attribute> <!-- 標簽名 --> <name>time</name> <!-- 是否為可選屬性 --> <required>false</required> <!-- 是否接受JSP表達示計算結果 --> <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表示不能包函內容的,scriptless,tagdependent --> <attribute> <!-- 標簽名 --> <name>times</name> <!-- 是否為可選屬性 --> <required>true</required> <!-- 是否接受JSP表達示計算結果 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
JSP頁面
<%@ 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 處理標記內容,并把這些內容寫到輸出流中doStartTag()
SKIP_BODY 不處理標記內容doStartTag(),doAfterBody()
EVAL_BODY_AGAIN 又重頭處理doAfterBody()
EVAL_PAGE 繼續執行JSP里面的代碼 doEndTag()
SKIP_PAGE 不繼續執行JSP里面的代碼 doEndTag()
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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