自定義標籤(TagSupport )

定義最簡單的標籤    
自定義標籤採用Default Adapter模式(缺省適配模式) java

Java代碼web

 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代碼session

 1  <?xml version="1.0" encoding="UTF-8" ?>    
 2  <taglib xmlns="http://java.sun.com/xml/ns/j2ee"    
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
 4      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"    
 5      version="2.0">    
 6      <tlib-version>1.0</tlib-version>    
 7      <short-name>util</short-name>    
 8      <uri>http://langhua.com/taglib/util</uri>    
 9      <tag>    
10          <name>timer</name>    
11          <tag-class>com.langhua.tagsupport.LangHuaTag</tag-class>    
12          <body-content>JSP</body-content>    
13          <!-- JSP,empty表示能能包函內容的,scriptless,tagdependent -->    
14      </tag>    
15           
16      <tag>    
17          <name>date</name>    
18          <tag-class>com.langhua.tagsupport.DateTag</tag-class>    
19          <body-content>empty</body-content>           
20          <!-- JSP,empty表示不能包函內容的,scriptless,tagdependent -->    
21          <attribute>    
22              <!-- 標籤名 -->    
23              <name>time</name>    
24              <!-- 是否爲可選屬性 -->    
25              <required>false</required>    
26              <!-- 是否接受JSP表達示計算結果 -->    
27              <rtexprvalue>true</rtexprvalue>    
28          </attribute>    
29          <attribute>    
30              <name>pattern</name>    
31              <required>true</required>    
32              <rtexprvalue>false</rtexprvalue>    
33          </attribute>    
34      </tag>    
35           
36      <tag>    
37          <name>loop</name>    
38          <tag-class>com.langhua.tagsupport.LoopTag</tag-class>    
39          <body-content>JSP</body-content>             
40          <!-- JSP,empty表示不能包函內容的,scriptless,tagdependent -->    
41          <attribute>    
42              <!-- 標籤名 -->    
43              <name>times</name>    
44              <!-- 是否爲可選屬性 -->    
45              <required>true</required>    
46              <!-- 是否接受JSP表達示計算結果 -->    
47              <rtexprvalue>true</rtexprvalue>    
48          </attribute>           
49      </tag>    
50  </taglib>    

JSP頁面    
Html代碼  less

 1  <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>    
 2      
 3  <util:timer></util:timer>    
 4  <util:loop times="3">    
 5      <util:date pattern="yyyy-MM-dd" /><br/>    
 6  </util:loop>    
 7    
 8  <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>  
 9    
10  <util:timer></util:timer>  
11  <util:loop times="3">  
12      <util:date pattern="yyyy-MM-dd" /><br/>  
13  </util:loop>

 

TagSupport的流程圖jsp

      SKIP_BODY 表示不用處理標籤體,直接調用doEndTag()方法。
   
    SKIP_PAGE 忽略標籤後面的JSP內容。
   
  EVAL_PAGE 處理標籤後,繼續處理JSP後面的內容。
   
  EVAL_BODY_BUFFERED 表示須要處理標籤體。
   
  EVAL_BODY_INCLUDE 表示須要處理標籤體,但繞過setBodyContent()和doInitBody()方法
   
  EVAL_BODY_AGAIN 對標籤體循環處理。ide

 

 

 

上面轉自http://zhuhuide2004.iteye.com/blog/555737oop

下面是項目中的應用。ui

在作權限的時候,不一樣角色用戶看到不一樣的權限,好比:一個頁面上的添加按鈕,有的角色看的到,有的角色看不到,必須判斷角色是否有這個權限。this

 1 public class MenuTLD extends TagSupport {
 2 
 3     
 4     private String menuStr;
 5     
 6     @Override
 7     public int doStartTag() throws JspException {
 8         // TODO Auto-generated method stub
 9         HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
10         List<SysMenuBean> userMenus = (List<SysMenuBean>) session.getAttribute("sysMenuBeans");
11         for (SysMenuBean sysMenuBean : userMenus) {
12             try {
13                 if(sysMenuBean.getUrl()!=null&&sysMenuBean.getUrl().contains(menuStr))
14                 {
15                     
16                     return TagSupport.EVAL_BODY_INCLUDE;
17                 }
18             } catch (Exception e) {
19                 // TODO Auto-generated catch block
20                 return TagSupport.SKIP_BODY;        
21             } 
22         }
23         return TagSupport.SKIP_BODY;        
24     }
25 
26     public String getMenuStr() {
27         return menuStr;
28     }
29 
30     public void setMenuStr(String menuStr) {
31         this.menuStr = menuStr;
32     }
33 
34 }

其內容是:spa

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>menu</short-name>
 <uri>http://www.dsdl.com/dev/jsp/menu</uri>
 <description>DSDL menu 1.1 core library</description>
 <tag>    
    <name>menu</name>
    <tag-class>com.dsdl.common.menutld.MenuTLD</tag-class>
    <body-content>JSP</body-content>
    <description>menu</description>
    <attribute>       
        <name>menuStr</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
        <type>type</type>
        <description>MenuId</description>
    </attribute>
  </tag>
</taglib>

在頁面中的應用:

相關文章
相關標籤/搜索