1.带有属性的处理类:
public class DomeTag extends SimpleTagSupport {
//1.声明属性的成员变量
private Integer num;
//2.关键点:必须提供公开的setter方法,给成员变量赋值;
//1.声明属性的成员变量
private Integer num;
//2.关键点:必须提供公开的setter方法,给成员变量赋值;
public void setNum(Integer num) {
this.num = num;
}
//3.覆盖doTag方法
@Override
public void doTag() throws JspException, IOException {
System.out.println("执行了标签");
//输出标签体内容
JspFragment jspBody = this.getJspBody();
this.num = num;
}
//3.覆盖doTag方法
@Override
public void doTag() throws JspException, IOException {
System.out.println("执行了标签");
//输出标签体内容
JspFragment jspBody = this.getJspBody();
//执行invoke方法,把标签体内容输出到指定的writer对象中
//1.2往浏览器输出内容,writer为null就是默认往浏览器输出
/*JspWriter out = this.getJspContext().getOut();
jspBody.invoke(out);
*/
jspBody.invoke(null);//等价上面的代码
//控制重复输出标签体的内容
//方法:多次调用invoke()方法
for(int i = 0; i <= num; i++){
jspBody.invoke(null);
}
//改变标签体的内容
//1.创建stringwriter临时容器
StringWriter sw = new StringWriter();
//2.把标签体拷贝到临时容器
jspBody.invoke(sw);
//3.从临时容器中得到标签体内容
String content = sw.toString();
//4.改变内容
content = content.toLowerCase();
System.out.println(content);
this.getJspContext().getOut().write(content);
throw new SkipPageException();
}
}
//1.2往浏览器输出内容,writer为null就是默认往浏览器输出
/*JspWriter out = this.getJspContext().getOut();
jspBody.invoke(out);
*/
jspBody.invoke(null);//等价上面的代码
//控制重复输出标签体的内容
//方法:多次调用invoke()方法
for(int i = 0; i <= num; i++){
jspBody.invoke(null);
}
//改变标签体的内容
//1.创建stringwriter临时容器
StringWriter sw = new StringWriter();
//2.把标签体拷贝到临时容器
jspBody.invoke(sw);
//3.从临时容器中得到标签体内容
String content = sw.toString();
//4.改变内容
content = content.toLowerCase();
System.out.println(content);
this.getJspContext().getOut().write(content);
throw new SkipPageException();
}
}
2.编辑TLD文件:
<?xml version = "1.0" encoding = "UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<!-- 标签库的版本 -->
<tlib-version>1.1</tlib-version>
<!-- 标签库前缀 -->
<short-name>test</short-name>
<!-- tld文件的唯一标记 -->
<uri>http://com.test</uri>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<!-- 标签库的版本 -->
<tlib-version>1.1</tlib-version>
<!-- 标签库前缀 -->
<short-name>test</short-name>
<!-- tld文件的唯一标记 -->
<uri>http://com.test</uri>
<!-- 一个标签的声明 -->
<tag>
<!-- 标签名称 -->
<name>showIp</name>
<!-- 标签处理器类的全名 -->
<tag-class>com.test.ShowIpTag</tag-class>
<!-- 输出标签体内容格式 -->
<body-content>scriptless</body-content>
</tag>
<!-- 一个标签的声明 -->
<tag>
<!-- 标签名称 -->
<name>DomeTag</name>
<!-- 标签处理器类的全名 -->
<tag-class>com.test.DomeTag</tag-class>
<!-- 输出标签体内容格式 -->
<body-content>scriptless</body-content>
<attribute>
<name>num</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
<tag>
<!-- 标签名称 -->
<name>showIp</name>
<!-- 标签处理器类的全名 -->
<tag-class>com.test.ShowIpTag</tag-class>
<!-- 输出标签体内容格式 -->
<body-content>scriptless</body-content>
</tag>
<!-- 一个标签的声明 -->
<tag>
<!-- 标签名称 -->
<name>DomeTag</name>
<!-- 标签处理器类的全名 -->
<tag-class>com.test.DomeTag</tag-class>
<!-- 输出标签体内容格式 -->
<body-content>scriptless</body-content>
<attribute>
<name>num</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
</taglib>
3.页面的使用:
4,运行结果: