義最簡單的標簽
自定義標簽采用Default Adapter模式(缺省適配模式)
Java代碼
Java代碼 
1 //最簡單的標簽
2 public class LangHuaTag extends TagSupport {
3 private long startTime;
4 private long endTime;
5
6 public int doStartTag() throws JspException {
7 startTime = System.currentTimeMillis();
8 //表示定制標記里面有所包括的JSP頁面
9 return TagSupport.EVAL_BODY_INCLUDE;
10 }
11 public int doEndTag() throws JspException {
12 endTime = System.currentTimeMillis();
13 long elapsed = endTime - startTime;
14 try {
15 JspWriter out = pageContext.getOut();
16 out.println("runtime is "+ elapsed);
17 } catch (IOException e) {
18 e.printStackTrace();
19 }
20 //表示JSP頁面繼續運行
21 return TagSupport.EVAL_PAGE;
22 }
23
24 }
25 //代屬性的標簽
26 public class DateTag extends TagSupport {
27 private String pattern = "yyyy-MM-dd hh:mm:ss";
28 private Date date;
29 //必須要有Set方法,因為是屬性可以設值
30 public void setPattern(String pattern) {
31 this.pattern = pattern;
32 }
33
34 public void setDate(Date date) {
35 this.date = date;
36 }
37
38 public int doEndTag() throws JspException {
39 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
40 //如果沒有就是當前時間
41 if(date==null){
42 date = new Date();
43 }
44 JspWriter out = pageContext.getOut();
45 try {
46 out.print(sdf.format(date));
47 } catch (IOException e) {
48 e.printStackTrace();
49 }
50 return TagSupport.EVAL_PAGE;
51 }
52 }
53
54 /**
55 * 循環輸出
56 * @author Administrator
57 *
58 */
59 public class LoopTag extends TagSupport {
60 private int times =0;
61 //Set方法設值
62 public void setTimes(int times) {
63 this.times = times;
64 }
65
66 public int doStartTag() throws JspException {
67 //表示定制標記里面有所包括的JSP頁面
68 return TagSupport.EVAL_BODY_INCLUDE;
69 }
70
71 public int doAfterBody() throws JspException {
72 if(times>0){
73 times--;
74 //表示雙從標簽開始輸入
75 return TagSupport.EVAL_BODY_AGAIN;
76 }
77 //表示結束,忽略標簽內部的內容
78 return TagSupport.SKIP_BODY;
79 }
80
81 }
配置文件
Xml代碼
Xml代碼 
82 <?xml version="1.0" encoding="UTF-8" ?>
83 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
84 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
85 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
86 version="2.0">
87 <tlib-version>1.0</tlib-version>
88 <short-name>util</short-name>
89 <uri>http://langhua.com/taglib/util</uri>
90 <tag>
91 <name>timer</name>
92 <tag-class>com.langhua.tagsupport.LangHuaTag</tag-class>
93 <body-content>JSP</body-content>
94 <!-- JSP,empty表示能能包函內容的,scriptless,tagdependent -->
95 </tag>
96
97 <tag>
98 <name>date</name>
99 <tag-class>com.langhua.tagsupport.DateTag</tag-class>
100 <body-content>empty</body-content>
101 <!-- JSP,empty表示不能包函內容的,scriptless,tagdependent -->
102 <attribute>
103 <!-- 標簽名 -->
104 <name>time</name>
105 <!-- 是否為可選屬性 -->
106 <required>false</required>
107 <!-- 是否接受JSP表達示計算結果 -->
108 <rtexprvalue>true</rtexprvalue>
109 </attribute>
110 <attribute>
111 <name>pattern</name>
112 <required>true</required>
113 <rtexprvalue>false</rtexprvalue>
114 </attribute>
115 </tag>
116
117 <tag>
118 <name>loop</name>
119 <tag-class>com.langhua.tagsupport.LoopTag</tag-class>
120 <body-content>JSP</body-content>
121 <!-- JSP,empty表示不能包函內容的,scriptless,tagdependent -->
122 <attribute>
123 <!-- 標簽名 -->
124 <name>times</name>
125 <!-- 是否為可選屬性 -->
126 <required>true</required>
127 <!-- 是否接受JSP表達示計算結果 -->
128 <rtexprvalue>true</rtexprvalue>
129 </attribute>
130 </tag>
131 </taglib>
JSP頁面
Html代碼
Html代碼 
132 <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>
133
134 <util:timer></util:timer>
135 <util:loop times="3">
136 <util:date pattern="yyyy-MM-dd" /><br/>
137 </util:loop>
138
139 <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>
140
141 <util:timer></util:timer>
142 <util:loop times="3">
143 <util:date pattern="yyyy-MM-dd" /><br/>
144 </util:loop>
TagSupport的流程圖
SKIP_BODY 表示不用處理標簽體,直接調用doEndTag()方法。
SKIP_PAGE 忽略標簽后面的JSP內容。
EVAL_PAGE 處理標簽后,繼續處理JSP后面的內容。
EVAL_BODY_BUFFERED 表示需要處理標簽體。
EVAL_BODY_INCLUDE 表示需要處理標簽體,但繞過setBodyContent()和doInitBody()方法
EVAL_BODY_AGAIN 對標簽體循環處理。
