JSP学习笔记(四十四):编写我的第一个自定义标签
目录WebRoot/WEB-INF下新建标签文件my.tld:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>modou</short-name>
<uri>/modou</uri>
<display-name>"Modou Tags"</display-name>
<tag>
<name>helloworld</name>
<tag-class>test.MyTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
建立对应的类文件MyTag.java,需要继承TagSupport类
package test;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class MyTag extends TagSupport {
@Override
public int doEndTag() throws JspException {
try {
pageContext.getOut().print("Hello World!");
} catch (Exception e) {
}
return EVAL_PAGE;
}
@Override
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}
}
现在在网页中就可以使用这个标签了:
<%@ taglib prefix="mytag" uri="/modou" %>
![](/images/dot.gif)
<mytag:helloworld/>