分享

自定义标签(TagSupport )

 大头祥子 2014-01-03

自定义标签采用Default Adapter模式(缺省适配模式) 

Java代码

Java代码 收藏代码
  1. //最简单的标签       

  2. public class LangHuaTag extends TagSupport {       

  3.     private long startTime;       

  4.     private long endTime;       

  5.     public int doStartTag() throws JspException {       

  6.         startTime = System.currentTimeMillis();       

  7.         //表示定制标记里面有所包括的JSP页面       

  8.         return TagSupport.EVAL_BODY_INCLUDE;       

  9.     }       

  10.     public int doEndTag() throws JspException {       

  11.         endTime = System.currentTimeMillis();       

  12.         long elapsed = endTime - startTime;            

  13.         try {       

  14.             JspWriter out = pageContext.getOut();       

  15.             out.println("runtime is "+ elapsed);       

  16.         } catch (IOException e) {       

  17.             e.printStackTrace();       

  18.         }       

  19.         //表示JSP页面继续运行       

  20.         return TagSupport.EVAL_PAGE;       

  21.     }       

  22. }       

  23. //代属性的标签       

  24. public class DateTag extends TagSupport {       

  25.     private String pattern = "yyyy-MM-dd hh:mm:ss";       

  26.     private Date date;       

  27.     //必须要有Set方法,因为是属性可以设值       

  28.     public void setPattern(String pattern) {       

  29.         this.pattern = pattern;       

  30.     }       

  31.     public void setDate(Date date) {       

  32.         this.date = date;       

  33.     }       

  34.     public int doEndTag() throws JspException {       

  35.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);       

  36.         //如果没有就是当前时间       

  37.         if(date==null){       

  38.             date = new Date();       

  39.         }              

  40.         JspWriter out = pageContext.getOut();       

  41.         try {       

  42.             out.print(sdf.format(date));       

  43.         } catch (IOException e) {       

  44.             e.printStackTrace();       

  45.         }       

  46.         return TagSupport.EVAL_PAGE;       

  47.     }       

  48. }       

  49. /**    

  50.  * 循环输出    

  51.  * @author Administrator    

  52.  *    

  53.  */      

  54. public class LoopTag extends TagSupport {       

  55.     private int times =0;       

  56.     //Set方法设值       

  57.     public void setTimes(int times) {       

  58.         this.times = times;       

  59.     }       

  60.     public int doStartTag() throws JspException {       

  61.         //表示定制标记里面有所包括的JSP页面       

  62.         return TagSupport.EVAL_BODY_INCLUDE;       

  63.     }       

  64.     public int doAfterBody() throws JspException {       

  65.         if(times>0){       

  66.             times--;       

  67.             //表示双从标签开始输入       

  68.             return TagSupport.EVAL_BODY_AGAIN;       

  69.         }          

  70.         //表示结束,忽略标签内部的内容       

  71.         return TagSupport.SKIP_BODY;       

  72.     }       

  73. }      

配置文件   
Xml代码

Xml代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>      

  2. <taglib xmlns="http://java./xml/ns/j2ee"      

  3.     xmlns:xsi="http://www./2001/XMLSchema-instance"      

  4.     xsi:schemaLocation="http://java./xml/ns/j2ee http://java./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:///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.     <tag>      

  16.         <name>date</name>      

  17.         <tag-class>com.langhua.tagsupport.DateTag</tag-class>      

  18.         <body-content>empty</body-content>             

  19.         <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->      

  20.         <attribute>      

  21.             <!-- 标签名 -->      

  22.             <name>time</name>      

  23.             <!-- 是否为可选属性 -->      

  24.             <required>false</required>      

  25.             <!-- 是否接受JSP表达示计算结果 -->      

  26.             <rtexprvalue>true</rtexprvalue>      

  27.         </attribute>      

  28.         <attribute>      

  29.             <name>pattern</name>      

  30.             <required>true</required>      

  31.             <rtexprvalue>false</rtexprvalue>      

  32.         </attribute>      

  33.     </tag>      

  34.     <tag>      

  35.         <name>loop</name>      

  36.         <tag-class>com.langhua.tagsupport.LoopTag</tag-class>      

  37.         <body-content>JSP</body-content>               

  38.         <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->      

  39.         <attribute>      

  40.             <!-- 标签名 -->      

  41.             <name>times</name>      

  42.             <!-- 是否为可选属性 -->      

  43.             <required>true</required>      

  44.             <!-- 是否接受JSP表达示计算结果 -->      

  45.             <rtexprvalue>true</rtexprvalue>      

  46.         </attribute>             

  47.     </tag>      

  48. </taglib>      

JSP页面   
Html代码  

Html代码 收藏代码
  1. <%@ taglib prefix="util" uri="http:///taglib/util"%>      

  2. <util:timer></util:timer>      

  3. <util:loop times="3">      

  4.     <util:date pattern="yyyy-MM-dd" /><br/>      

  5. </util:loop>      

  6. <%@ taglib prefix="util" uri="http:///taglib/util"%>    

  7. <util:timer></util:timer>    

  8. <util:loop times="3">    

  9.     <util:date pattern="yyyy-MM-dd" /><br/>    

  10. </util:loop>  

TagSupport的流程图

          SKIP_BODY 表示不用处理标签体,直接调用doEndTag()方法。
      
        SKIP_PAGE 忽略标签后面的JSP内容。
      
      EVAL_PAGE 处理标签后,继续处理JSP后面的内容。
      
      EVAL_BODY_BUFFERED 表示需要处理标签体。
      
      EVAL_BODY_INCLUDE 表示需要处理标签体,但绕过setBodyContent()和doInitBody()方法
      
      EVAL_BODY_AGAIN 对标签体循环处理。

        本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
        转藏 分享 献花(0

        0条评论

        发表

        请遵守用户 评论公约

        类似文章 更多