1、实现Tag接口:

TagSupport类实现了Tag接口,为我们提供了4个重要的方法(见表6-5)。

1.1、 TagSupport类中的常用方法

          int doStartTag():

遇到自定义标签开始时调用该方法,

其可选返回值如下。

SKIP_BODY:表示不用处理标签体,

直接调用doEndTag()方法

EVAL_BODY_INCLUDE:正常执行

标签体,但不对标签体做任何处理

int doAfterBody():

重复执行标签体内容的方法,
其可选返回值如下。SKIP_BODY:表示不用处理标签体,直接调用doEndTag()方法EVAL_BODY_AGAIN:重复执行标签体内容

int doEndTag():

遇到自定义标签结束时调用该方法,

其可选返回值如下。

SKIP_PAGE:忽略标签后面的JSP

内容,中止JSP页面执行

EVAL_PAGE:处理标签后,继

续处理JSP后面的内容

void release():

释放获得的所有资源

参考代码:

package com.rhythmk.web.tag;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport; /*
* 输出当前时间
* */
public class ViewTimeTag extends TagSupport { @Override
public int doStartTag() throws JspException { HttpServletRequest request=(HttpServletRequest) this.pageContext.getRequest();
JspWriter out=this.pageContext.getOut(); Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try {
out.print(df.format(date));
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
} return super.doStartTag();
}
}
 
 

2、创建 *.tld文件:

路径:/WebRoot/WEB-INF/rhythmk.tld

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>rhythmk</short-name>
<uri>http://www.rhythmk.com</uri>
<description>rhythmk library</description> <tag>
<name>ViewTime</name>
<tag-class>com.rhythmk.web.tag.ViewTimeTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>

3、引入标记到JSP页码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib  uri="http://www.rhythmk.com"  prefix="rhythmk"  %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JSP 自定义标签 </title>
</head> <body>
当前时间为:
<rhythmk:ViewTime/>
</body>
</html>

简单标签体开发:

继承 “SimpleTagSupport”  实现 doTag 方法:

package com.rhythmk.web.tag;

import java.io.IOException;
import java.io.StringWriter; import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport; /*
* 简单标签
* */ public class SimpleTagDemo1 extends SimpleTagSupport { @Override
public void doTag() throws JspException, IOException { JspFragment jf=this.getJspBody();
/* 循环输出标签内容5次 */
for(int i=0;i<5;i++)
{
jf.invoke(null);
} // 修改标签内容 StringWriter sw=new StringWriter();
// 获取标签内容
jf.invoke(sw);
String content= "<br/><b>"+ sw.toString()+"被修改了<b/>";
// 写入页面
this.getJspContext().getOut().write(content); } }

配置如下:

...... 

<tag>
<name>SimpleTagDemo1</name>
<tag-class>com.rhythmk.web.tag.SimpleTagDemo1</tag-class>
<body-content>scriptless</body-content>
</tag> ......

调用方法:

<%@ taglib  uri="http://www.rhythmk.com"  prefix="rhythmk"  %>
<rhythmk:SimpleTagDemo1>
标签内容
</rhythmk:SimpleTagDemo1>

最新文章

  1. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
  2. RabbitMQ常用命令
  3. nginx 配置文件分析以及配置负载均衡器
  4. TAROT.
  5. 【bzoj1059】矩形游戏
  6. WITH SCHEMABINDING
  7. How does java technology relate to cloud computing?
  8. poj 3358 Period of an Infinite Binary Expansion
  9. [转]让程序在崩溃时体面的退出之SEH+Dump文件
  10. VC2013 添加库文件
  11. Linux下对字符串进行MD5加密
  12. 日志之再说Log4J
  13. POJ(1195)(单点修改,区间查询)(二维)
  14. EF时,数据库字段和实体类不一致问题
  15. C# -- 使用Ping检查网络是否正常
  16. [Java] SpringMVC工作原理之一:DispatcherServlet
  17. SQL 查看表字段及说明
  18. open-falcon实现邮件报警
  19. python学习 day016打卡 面向对象--成员
  20. DOM confirm setTimeout url刷新

热门文章

  1. erlang游戏开发tcp
  2. windows中的oracle12SE后启动的系统服务的列表
  3. 推荐sinaapp谷歌搜索引擎,firefox自定义搜索引擎
  4. HDU1800 hash+去前导0
  5. threejs Object的点击(鼠标)事件(获取点击事件的object)
  6. LeetCode 773. Sliding Puzzle
  7. 后缀数组模板/LCP模板
  8. spring加载bean报错:expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
  9. string源码实现分析
  10. Python学习笔记第一讲