ITeye博客

JSP自定义标签 实例

dong_java · 2009-07-10 15:12 · Markdown

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

 

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. }
//最简单的标签
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. <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>
<?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. <util:timer></util:timer>
  3. <util:loop times="3">
  4. <util:date pattern="yyyy-MM-dd" /><br/>
  5. </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)

  1. #2 楼 · cf2huihui · 2013-09-23 15:16

    谢谢 , 解决了我的问题

  2. #1 楼 · love_miaohong · 2012-11-21 22:27

    :idea: 谢谢分享! 描述很清晰

评论功能维护中,当前仅展示历史评论。