使用简单标签同样可以完成传统标签可完成的功能:
一个所有实例的tld描述文件:
<?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/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A taglibrary exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>csdn</short-name>
<uri>http://www.hbsi.simple</uri>
<tag>
<description>OutputsHello, World</description>
<name>s1</name>
<tag-class>www.hbsi.web.simptag.SimpleTag1</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>s2</name>
<tag-class>www.hbsi.web.simptag.SimpleTag2</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>s3</name>
<tag-class>www.hbsi.web.simptag.SimpleTag3</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>s4</name>
<tag-class>www.hbsi.web.simptag.SimpleTag4</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>s5</name>
<tag-class>www.hbsi.web.simptag.SimpleTag5</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<description>OutputsHello, World</description>
<name>s6</name>
<tag-class>www.hbsi.web.simptag.SimpleTag6</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
</tag>
</taglib>
相应的标签处理类及jsp引用文件:
1、 实现简单输出
publicclass SimpleTag1 extends SimpleTagSupport {
@Override
publicvoid doTag() throws JspException,IOException {
JspFragment jf = this.getJspBody();
JspContext jc = jf.getJspContext();
jf.invoke(jc.getOut());
}
}
Jsp引用:
<%@ tagliburi="http://www.hbsi.simple" prefix ="csdn" %>
<body>
<csdn:s1>
aaaaaaaa<br>
</csdn:s1>
</body>
效果:
2、 决定标签体以后的内容是否输出
publicclass SimpleTag2 extends SimpleTagSupport {
@Override
publicvoid doTag() throws JspException,IOException {
JspFragment jf = this.getJspBody();
jf.invoke(null);
// 决定标签体以后的内容是否输出
thrownew SkipPageException();
}
}
Jsp标签引用:
<%@taglib uri="http://www.hbsi.simple" prefix ="csdn" %>
<body>
<csdn:s2>
aaaaaaaa<br>
</csdn:s2>
cccccccccccc<br/>
</body>
效果:
3、 决定标签体以后的内容是否循环输出
publicclass SimpleTag3 extendsSimpleTagSupport {
@Override
publicvoid doTag() throwsJspException, IOException {
// 决定标签体以后的内容是否循环输出
JspFragment jf = this.getJspBody();
for(int i=0;i<3;i++){
jf.invoke(null);
}
}
}
Jsp文件引用:
<%@ taglib uri="http://www.hbsi.simple" prefix="csdn" %>
<body>
<csdn:s3>
aaaaaaaa<br>
</csdn:s3>
</body>
效果如图:
4、 决定标签体以后的内容是否修改输出
publicclass SimpleTag4 extends SimpleTagSupport {
@Override
publicvoid doTag() throws JspException,IOException {
// 决定标签体以后的内容是否修改输出
JspFragment jf = this.getJspBody();
StringWriter sw = newStringWriter();
jf.invoke(sw);
String content = sw.toString();
String uc = content.toUpperCase();
JspContext js=jf.getJspContext();
js.getOut().write(uc);
}
}
Jsp文件引用:
<%@ tagliburi="http://www.hbsi.simple" prefix ="csdn" %>
<body>
<csdn:s4>
aaaaaaaa<br>
</csdn:s4>
</body>
效果如图:
5、 修改日期然后输出
publicclass SimpleTag5 extends SimpleTagSupport {
@Override
publicvoid doTag() throws JspException,IOException {
// 决定标签体以后的内容是否修改输出
JspFragment jf = this.getJspBody();
StringWriter sw = newStringWriter();
jf.invoke(sw);
String content = sw.toString();
String sc []=content.split("-");
JspContext js=jf.getJspContext();
js.getOut().write(sc[0]+"<br>");
js.getOut().write(sc[1]+"<br>");
js.getOut().write(sc[2]+"<br>");
}
}
Jsp引用文件:
<%@ taglib uri="http://www.hbsi.simple"prefix ="csdn" %>
<body>
<csdn:s5>
1991-10-13<br>
</csdn:s5>
</body>
效果:
好了赶快自己试一试吧!