定义标签的操作类----HelloTag.java
package org.tagdemo ;
import javax.servlet.jsp.* ;
import javax.servlet.jsp.tagext.* ;
public class HelloTag extends TagSupport{
public int doStartTag() throws JspException{
JspWriter out = super.pageContext.getOut() ;
try{
out.println("<h1>Hello World!!!</h1>") ;
}catch(Exception e){}
return TagSupport.SKIP_BODY ;
}
}
定义标签的描述文件----/WEB-INF/hellotag.tld
<?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_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>firsttag</short-name>
<tag>
<name>hello</name>
<tag-class>org.tagdemo.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
编写JSP页面并调用标签----hellotag.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="mytag" uri="mldn_hello"%>
<html>
<head><title></title></head>
<body>
<h1><mytag:hello/></h1>
</body>
</html>
一般可以再web.xml文件对所有的*.tld文件进行名称的映射,以后直接在JSP页面中使用映射名称即可访问标签描述文件。
修改web.xml,映射TLD文件
<jsp-config>
<taglib>
<taglib-uri>mldn_hello</taglib-uri>
<taglib-location>/WEB-INF/hellotag.tld</taglib-location>
</taglib>
</jsp-config>
定义有属性的标签
格式化日前标签类----DateTag.java
package org.tagdemo ;
import java.text.* ;
import java.util.* ;
import javax.servlet.jsp.* ;
import javax.servlet.jsp.tagext.* ;
public class DateTag extends TagSupport {
private String format ; // 当设置属性的时候可以通过setter完成
public int doStartTag()
throws JspException{
SimpleDateFormat sdf = new SimpleDateFormat(this.format) ;
// 表示进行格式化的日期显示操作
try{
super.pageContext.getOut().write(sdf.format(new Date())) ;
}catch(Exception e){
e.printStackTrace() ; // 异常处理操作
}
return TagSupport.SKIP_BODY ;
}
public void setFormat(String format){
this.format = format ;
}
public String getFormat(){
return this.format ;
}
}
定义标签描述文件----/WEB-INF/datatag.tld
<?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_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>datetag</short-name>
<tag>
<name>date</name>
<tag-class>org.tagdemo.DateTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>format</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
注:
<attribute>:表示定义一个标签中所具备的属性,一个<tag>元素中可以定义多个<attribute>元素
<name>:标签属性的名称
<required>:此属性是否为必须设置,如果为true,则表示必须设置;如果为false,则表示可选。
<rtexprvalue>:是否支持表达式输出,true表示支持,false表示不支持。
配置web.xml
<jsp-config>
<taglib>
<taglib-uri>mldn_date</taglib-uri>
<taglib-location>/WEB-INF/datetag.tld</taglib-location>
</taglib>
</jsp-config>
格式化日期----datetag.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="mytag" uri="mldn_date"%>
<html>
<head><title></title></head>
<body>
<h1><mytag:date format="yyyy-MM-dd HH:mm:ss.SSS"/></h1>
</body>
</html>
关于TLD中的<rtexprvalue>元素
<rtexprvalue>元素的主要功能是定义format属性是否支持表达式输出,如果设置成true,则可以通过如下的代码形式设置format属性的内容:
<%pageContext.setAttribute("fm","yyyy-MM-dd HH:mm:ss.SSS"); %>
<mytag:date format="${fm}"/>
或者使用“<%=%>”的形式输出format属性的内容:
<% String str = "yyyy-MM-dd HH:mm:ss.SSS"; %>
<mytag:date format="<%=str%>"/>
而如果将此属性设置成了false,则是无法在此处使用表达式输出的,否则程序将出现异常。