总所周知,JSP自定义标签库,主要是为了去掉JSP页面中的JAVA语句
此处以格式化输出时间戳为指定日期格式为例,简单介绍下JSP自定义标签的过程。
- 编写标签处理类(可继承自javax.servlet.jsp.tagext.TagSupport),主要重写方法doStartTag()即可。
- 创建TLD文件(在WEB-INF目录下创建)。
- JSP页面中使用标签
1、编写标签处理类(继承自TagSupport.java ):FormatDate.java (类中实例变量 value, patter与TLD文件中的标签属性对应,同时需要定义相应的getter/setter方法用于获取jsp页面的 值)
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import com.tianwen.dcdp.common.General; public class FormatDate extends TagSupport { private static final long serialVersionUID = -4328438154019437198L; private String value; private String pattern; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getPattern() { return pattern; } public void setPattern(String pattern) { this.pattern = pattern; } @Override public int doStartTag() throws JspException { if(General.isEmpty(value)) { value = "0"; } SimpleDateFormat format = new SimpleDateFormat(General.isEmpty(pattern) ? "yyyy-MM-dd" : pattern); String result = format.format(new Date(Long.parseLong(value))); try { this.pageContext.getOut().write(result); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } }
2、定义TLD文件 :formatTag.tld (此处标签名为date, uri用于稍后再jsp页面引入)
<?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.1</tlib-version> <jsp-version>1.2</jsp-version> <short-name>cl</short-name> <uri>/mytag1</uri> <tag> <name>date</name> <tag-class>com.tianwen.dcdp.FormatDate</tag-class> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pattern</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
如上所述,pattern属性定义为了非必须(<required>false</required>)的,意味着,后续再JSP中使用时可以不指定该属性。
需要注意的是 :
若上述TLD文件中没有配置uri属性,我们还可以在web.xml中按如下方式配置(若已配置可省略如下步骤):
<jsp-config> <taglib>< <taglib-uri>/mytag1</taglib-uri> <taglib-location>/WEB-INF/formatTag.tld</taglib-location> </taglib> </jsp-config>
3、jsp页面中使用自定义标签
首先需要引入相应的标签库(uri与标签库TLD文件中 的uri对应),如下所示:
<%@ taglib uri="/mytag1" prefix="cl"%>
然后便可以在JSP中使用该标签库中的标签了(此处只定义了一个标签“date”, 实际开发中可定义多个tag标签),如下所示:
<cl:date value="1356236547896" pattern="yyyy-MM-dd HH-mm-ss"></cl:date> <cl:date value="1356236547896"></cl:date>
因为pattern为非必须属性,所以上述两种用法均可以。
到此,jsp自定义标签的开发与使用介绍完毕。
最后介绍下,编写标签处理类时,父类(TagSupport.java)中的三个方法:
先看下该类所实现的接口Tag中的常量声明:
/** * Skip body evaluation. * Valid return value for doStartTag and doAfterBody. */ public final static int SKIP_BODY = 0; /** * Evaluate body into existing out stream. * Valid return value for doStartTag. */ public final static int EVAL_BODY_INCLUDE = 1; /** * Skip the rest of the page. * Valid return value for doEndTag. */ public final static int SKIP_PAGE = 5; /** * Continue evaluating the page. * Valid return value for doEndTag(). */ public final static int EVAL_PAGE = 6;
下面接着看第一个方法:doStartTag(),默认实现如下(不显示标签体内容)
/** * Default processing of the start tag, returning SKIP_BODY. * * @return SKIP_BODY * @throws JspException if an error occurs while processing this tag * * @see Tag#doStartTag() */ public int doStartTag() throws JspException { return SKIP_BODY; }
该方法是遇到开始标签是被调用,通过上述接口中的常量声明可以看出,其合法的返回值是:SKIP_BODY = 0 和 EVAL_BODY_INCLUDE = 1 前者表示将显示标签间(标签体)的文字,后者表示不显示标签间的文字,由此可见默认的实现为:不显示标签体的内容,若我们在编写自己的标签处理类时,若需要显示标签体,则在重写方法doStartTag()时修改返回值为:EVAL_BODY_INCLUDE
紧接着介绍第二个方法:doEndTag()默认实现如下(处理完标签后继续执行以下的JSP网页),通常不需要重写该方法
/** * Default processing of the end tag returning EVAL_PAGE. * * @return EVAL_PAGE * @throws JspException if an error occurs while processing this tag * * @see Tag#doEndTag() */ public int doEndTag() throws JspException { return EVAL_PAGE; }
该方法遇到结束标签时被调用,通过上述接口中的常量声明可以看出,其合法的返回值是:SKIP_PAGE = 5 和 EVAL_PAGE = 6 前者表示处理完标签后继续执行以下的JSP网页,后者是表示不处理接下来的JSP网页(可重写该方法实现防倒链功能)。
下面介绍最后一个方法 :doAfterBody(),通常不需重写该方法
/** * Default processing for a body. * * @return SKIP_BODY * @throws JspException if an error occurs while processing this tag * * @see IterationTag#doAfterBody() */ public int doAfterBody() throws JspException { return SKIP_BODY; }
该方法是在显示完标签间文字之后呼叫的.常用于迭代