首先需要一个标签类
首先需要继承TagSupport 实现它其中的一个方法doStartTag()
public class MyTag extends TagSupport {
@Override
public int doStartTag ( ) throws JspException {
JspWriter out = super . pageContext. getOut ( ) ;
try {
out. print ( "<span>这个是自定义标签输出的内容</span>" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
return super . doStartTag ( ) ;
}
书写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.0</ tlib-version>
< jsp-version> 1.2</ jsp-version>
< short-name> myTag</ short-name>
< uri> http://xujiangchen/myTag/</ uri>
< display-name> Common Tag</ display-name>
< description> Common Tag library</ description>
< tag>
< name> out</ name>
< tag-class> com.xu.crm.utils.MyTag</ tag-class>
< body-content> JSP</ body-content>
</ tag>
</ taglib>
页面
<%@ taglib prefix="myTag" uri="http://xujiangchen/myTag/"%>
< myTag: out> </ myTag: out>