`
dong_java
  • 浏览: 42102 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

JSP自定义标签 实例

阅读更多
定义最简单的标签
自定义标签采用Default Adapter模式(缺省适配模式)

 

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. }  
//最简单的标签
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代码
  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>  
<?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页面
Html代码
  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>  
<%@ 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()

分享到:
评论
2 楼 cf2huihui 2013-09-23  
谢谢 ,  解决了我的问题
1 楼 love_miaohong 2012-11-21  
  谢谢分享! 描述很清晰

相关推荐

Global site tag (gtag.js) - Google Analytics