标签:extends 技术 turn htm extend 9.png 大神 xmlns ret
只看原理就好,我做失败了!!!
报错:请大神帮我看看

首先先写好实现这个标签的java类,这个类要继承tagsupput方法
来看下api:所以我们重写doStartTag就行

下面是我写好的java类:

public class Snowing extends TagSupport {
@Override
public int doStartTag() throws JspException {
JspWriter out=this.pageContext.getOut();
Date date=new Date();
String time=date.toLocaleString();
try {
out.println(time);;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
}
写好java类之后,我们来把他绑定到一个uri上:在snowing.tld中
这个的格式tomcat中有例子,位置如下:

snowing.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/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>timeNow</short-name>
<uri>snowing.com</uri>
<tag>
<name>timeNow</name>
<tag-class>a.Snowing</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
绑定后:在jsp页面引用

编译后的源码:

标签:extends 技术 turn htm extend 9.png 大神 xmlns ret
原文地址:http://www.cnblogs.com/SnowingYXY/p/6713694.html