TagSupport使用入门

来源:互联网 发布:烤红薯店加盟骗局知乎 编辑:程序博客网 时间:2024/05/16 07:07

Java代码  收藏代码
  1. 定义最简单的标签   
  2. 自定义标签采用Default Adapter模式(缺省适配模式)   
  3. Java代码   
  4. //最简单的标签     
  5. public class LangHuaTag extends TagSupport {     
  6.     private long startTime;     
  7.     private long endTime;     
  8.          
  9.     public int doStartTag() throws JspException {     
  10.         startTime = System.currentTimeMillis();     
  11.         //表示定制标记里面有所包括的JSP页面     
  12.         return TagSupport.EVAL_BODY_INCLUDE;     
  13.     }     
  14.     public int doEndTag() throws JspException {     
  15.         endTime = System.currentTimeMillis();     
  16.         long elapsed = endTime - startTime;          
  17.         try {     
  18.             JspWriter out = pageContext.getOut();     
  19.             out.println("runtime is "+ elapsed);     
  20.         } catch (IOException e) {     
  21.             e.printStackTrace();     
  22.         }     
  23.         //表示JSP页面继续运行     
  24.         return TagSupport.EVAL_PAGE;     
  25.     }     
  26.          
  27. }     
  28. //代属性的标签     
  29. public class DateTag extends TagSupport {     
  30.     private String pattern = "yyyy-MM-dd hh:mm:ss";     
  31.     private Date date;     
  32.     //必须要有Set方法,因为是属性可以设值     
  33.     public void setPattern(String pattern) {     
  34.         this.pattern = pattern;     
  35.     }     
  36.          
  37.     public void setDate(Date date) {     
  38.         this.date = date;     
  39.     }     
  40.     
  41.     public int doEndTag() throws JspException {     
  42.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);     
  43.         //如果没有就是当前时间     
  44.         if(date==null){     
  45.             date = new Date();     
  46.         }            
  47.         JspWriter out = pageContext.getOut();     
  48.         try {     
  49.             out.print(sdf.format(date));     
  50.         } catch (IOException e) {     
  51.             e.printStackTrace();     
  52.         }     
  53.         return TagSupport.EVAL_PAGE;     
  54.     }     
  55. }     
  56.     
  57. /**   
  58.  * 循环输出   
  59.  * @author Administrator   
  60.  *   
  61.  */    
  62. public class LoopTag extends TagSupport {     
  63.     private int times =0;     
  64.     //Set方法设值     
  65.     public void setTimes(int times) {     
  66.         this.times = times;     
  67.     }     
  68.          
  69.     public int doStartTag() throws JspException {     
  70.         //表示定制标记里面有所包括的JSP页面     
  71.         return TagSupport.EVAL_BODY_INCLUDE;     
  72.     }     
  73.          
  74.     public int doAfterBody() throws JspException {     
  75.         if(times>0){     
  76.             times--;     
  77.             //表示双从标签开始输入     
  78.             return TagSupport.EVAL_BODY_AGAIN;     
  79.         }        
  80.         //表示结束,忽略标签内部的内容     
  81.         return TagSupport.SKIP_BODY;     
  82.     }     
  83.          
  84. }    
  85.   
  86. //最简单的标签  
  87. public class LangHuaTag extends TagSupport {  
  88.     private long startTime;  
  89.     private long endTime;  
  90.       
  91.     public int doStartTag() throws JspException {  
  92.         startTime = System.currentTimeMillis();  
  93.         //表示定制标记里面有所包括的JSP页面  
  94.         return TagSupport.EVAL_BODY_INCLUDE;  
  95.     }  
  96.     public int doEndTag() throws JspException {  
  97.         endTime = System.currentTimeMillis();  
  98.         long elapsed = endTime - startTime;       
  99.         try {  
  100.             JspWriter out = pageContext.getOut();  
  101.             out.println("runtime is "+ elapsed);  
  102.         } catch (IOException e) {  
  103.             e.printStackTrace();  
  104.         }  
  105.         //表示JSP页面继续运行  
  106.         return TagSupport.EVAL_PAGE;  
  107.     }  
  108.       
  109. }  
  110. //代属性的标签  
  111. public class DateTag extends TagSupport {  
  112.     private String pattern = "yyyy-MM-dd hh:mm:ss";  
  113.     private Date date;  
  114.     //必须要有Set方法,因为是属性可以设值  
  115.     public void setPattern(String pattern) {  
  116.         this.pattern = pattern;  
  117.     }  
  118.       
  119.     public void setDate(Date date) {  
  120.         this.date = date;  
  121.     }  
  122.   
  123.     public int doEndTag() throws JspException {  
  124.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  125.         //如果没有就是当前时间  
  126.         if(date==null){  
  127.             date = new Date();  
  128.         }         
  129.         JspWriter out = pageContext.getOut();  
  130.         try {  
  131.             out.print(sdf.format(date));  
  132.         } catch (IOException e) {  
  133.             e.printStackTrace();  
  134.         }  
  135.         return TagSupport.EVAL_PAGE;  
  136.     }  
  137. }  
  138.   
  139. /** 
  140.  * 循环输出 
  141.  * @author Administrator 
  142.  * 
  143.  */  
  144. public class LoopTag extends TagSupport {  
  145.     private int times =0;  
  146.     //Set方法设值  
  147.     public void setTimes(int times) {  
  148.         this.times = times;  
  149.     }  
  150.       
  151.     public int doStartTag() throws JspException {  
  152.         //表示定制标记里面有所包括的JSP页面  
  153.         return TagSupport.EVAL_BODY_INCLUDE;  
  154.     }  
  155.       
  156.     public int doAfterBody() throws JspException {  
  157.         if(times>0){  
  158.             times--;  
  159.             //表示双从标签开始输入  
  160.             return TagSupport.EVAL_BODY_AGAIN;  
  161.         }     
  162.         //表示结束,忽略标签内部的内容  
  163.         return TagSupport.SKIP_BODY;  
  164.     }  
  165.       
  166. }  
  167.   
  168.   
  169.   
  170.   
  171. 配置文件   
  172. Xml代码   
  173. <?xml version="1.0" encoding="UTF-8" ?>    
  174. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"    
  175.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  176.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"    
  177.     version="2.0">    
  178.     <tlib-version>1.0</tlib-version>    
  179.     <short-name>util</short-name>    
  180.     <uri>http://langhua.com/taglib/util</uri>    
  181.     <tag>    
  182.         <name>timer</name>    
  183.         <tag-class>com.langhua.tagsupport.LangHuaTag</tag-class>    
  184.         <body-content>JSP</body-content>    
  185.         <!-- JSP,empty表示能能包函内容的,scriptless,tagdependent -->    
  186.     </tag>    
  187.          
  188.     <tag>    
  189.         <name>date</name>    
  190.         <tag-class>com.langhua.tagsupport.DateTag</tag-class>    
  191.         <body-content>empty</body-content>           
  192.         <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->    
  193.         <attribute>    
  194.             <!-- 标签名 -->    
  195.             <name>time</name>    
  196.             <!-- 是否为可选属性 -->    
  197.             <required>false</required>    
  198.             <!-- 是否接受JSP表达示计算结果 -->    
  199.             <rtexprvalue>true</rtexprvalue>    
  200.         </attribute>    
  201.         <attribute>    
  202.             <name>pattern</name>    
  203.             <required>true</required>    
  204.             <rtexprvalue>false</rtexprvalue>    
  205.         </attribute>    
  206.     </tag>    
  207.          
  208.     <tag>    
  209.         <name>loop</name>    
  210.         <tag-class>com.langhua.tagsupport.LoopTag</tag-class>    
  211.         <body-content>JSP</body-content>             
  212.         <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->    
  213.         <attribute>    
  214.             <!-- 标签名 -->    
  215.             <name>times</name>    
  216.             <!-- 是否为可选属性 -->    
  217.             <required>true</required>    
  218.             <!-- 是否接受JSP表达示计算结果 -->    
  219.             <rtexprvalue>true</rtexprvalue>    
  220.         </attribute>           
  221.     </tag>    
  222. </taglib>    
  223.   
  224. <?xml version="1.0" encoding="UTF-8" ?>  
  225. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  226.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  227.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
  228.     version="2.0">  
  229.     <tlib-version>1.0</tlib-version>  
  230.     <short-name>util</short-name>  
  231.     <uri>http://langhua.com/taglib/util</uri>  
  232.     <tag>  
  233.         <name>timer</name>  
  234.         <tag-class>com.langhua.tagsupport.LangHuaTag</tag-class>  
  235.         <body-content>JSP</body-content>  
  236.         <!-- JSP,empty表示能能包函内容的,scriptless,tagdependent -->  
  237.     </tag>  
  238.       
  239.     <tag>  
  240.         <name>date</name>  
  241.         <tag-class>com.langhua.tagsupport.DateTag</tag-class>  
  242.         <body-content>empty</body-content>        
  243.         <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->  
  244.         <attribute>  
  245.             <!-- 标签名 -->  
  246.             <name>time</name>  
  247.             <!-- 是否为可选属性 -->  
  248.             <required>false</required>  
  249.             <!-- 是否接受JSP表达示计算结果 -->  
  250.             <rtexprvalue>true</rtexprvalue>  
  251.         </attribute>  
  252.         <attribute>  
  253.             <name>pattern</name>  
  254.             <required>true</required>  
  255.             <rtexprvalue>false</rtexprvalue>  
  256.         </attribute>  
  257.     </tag>  
  258.       
  259.     <tag>  
  260.         <name>loop</name>  
  261.         <tag-class>com.langhua.tagsupport.LoopTag</tag-class>  
  262.         <body-content>JSP</body-content>          
  263.         <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->  
  264.         <attribute>  
  265.             <!-- 标签名 -->  
  266.             <name>times</name>  
  267.             <!-- 是否为可选属性 -->  
  268.             <required>true</required>  
  269.             <!-- 是否接受JSP表达示计算结果 -->  
  270.             <rtexprvalue>true</rtexprvalue>  
  271.         </attribute>        
  272.     </tag>  
  273. </taglib>  
  274.   
  275.   
  276. JSP页面   
  277. Html代码   
  278. <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>    
  279.     
  280. <util:timer></util:timer>    
  281. <util:loop times="3">    
  282.     <util:date pattern="yyyy-MM-dd" /><br/>    
  283. </util:loop>    
  284.   
  285. <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>  
  286.   
  287. <util:timer></util:timer>  
  288. <util:loop times="3">  
  289.     <util:date pattern="yyyy-MM-dd" /><br/>  
  290. </util:loop>  
  291.   
  292. TagSupport的流程图   
  293.    
  294. EVAL_BODY_INCLUDE 处理标记内容,并把这些内容写到输出流中doStartTag()   
  295. SKIP_BODY 不处理标记内容doStartTag(),doAfterBody()   
  296. EVAL_BODY_AGAIN 又重头处理doAfterBody()   
  297. EVAL_PAGE 继续执行JSP里面的代码 doEndTag()   
  298. SKIP_PAGE 不继续执行JSP里面的代码 doEndTag()   
  299.   
  300. [img]http://langhua9527.iteye.com/upload/attachment/77593/2ce5c561-a5ea-36d1-9082-25e62273dcb2.jpg[/img] 
0 0
原创粉丝点击