JSP自定义标签-定制URI
1 JSP自定义标签的定制URI
默认情况下JSP自定义标签的tld文件存放在WEB-INF目录下,但是我们可以使用自定义URI来改变tld文件存放的位置。重点是,需要在web.xml声明tld文件存放的地方。
2 JSP自定义标签定制URI的示例
在下面的示例中,我们将在JSP文件中使用自定义URI。
2.1 编写标签处理程序
PrintDate:
package com.yiidian;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class PrintDate extends TagSupport{
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();
try{
out.print(java.util.Calendar.getInstance().getTime());
}catch(Exception e){e.printStackTrace();}
return SKIP_BODY;
}
}
2.2 编写mytags.tld
mytags.tld:
/p>
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
1.0
1.2
simple
mytags
A simple tab library for the examples
today
com.yiidian.PrintDate
2.3 配置web.xml
web.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
mytags
/WEB-INF/mytags.tld
这里的配置是重点,指定mytags.tld文件存放的位置。
2.4 编写index.jsp
index.jsp:
一点教程网-JSP自定义标签定制URI的示例今天是:
2.5 运行测试