1、自定义标签主要分三步,有时只需两步
a、创建标签类
b、创建对应标签tld文件,并放置在WEB-INF下(可打成 jar 包)
c、在web.xml里配置文件 tld 的路径(servlet2.4 之后的版本都不需要配置了)
2、创建标签类
a、一般继承标签支持类SimpleTagSupport,重写doTag()方法,在方法体里书写自定义页面元素信息。
public class TeamPageTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
StringBuffer sb = new StringBuffer();
...
...
getJspContext().getOut().println(sb.toString());
}
}
3、创建tld文件,并指定JSP页面引用的 URI
<?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.0</tlib-version>
<!-- 标签库的默认前缀 -->
<short-name>page</short-name>
<!-- 标签库的默认URI -->
<uri>teamPageTag</uri>
<tag>
<!-- jsp页面引用 标签名称 -->
<name>pageList</name>
<!-- 类路径及名称 -->
<tag-class>com.lz.tag.TeamPageTag</tag-class>
<!-- 指定标签内容 ,这里指定的是空标签-->
<body-content>empty</body-content>
<attribute>
<description>当前页</description> <!-- 描述该字段的意思 -->
<name>pageNum</name> <!-- 参数名称 -->
<required>true</required> <!-- 是否为必传项 -->
<rtexprvalue>true</rtexprvalue> <!-- 是否可以使用表达式 -->
</attribute>
<attribute>
<description>路径附带参数 (形式:一个参时 : 键=值 ,多个参数时 : 键=值 与号 键=值)</description>
<name>urlParameter</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue> <!-- 是否可以使用表达式 -->
</attribute>
</tag>
</taglib>