自定义一个很简单的JSP标签
web 工程,新建java类:
[java] package leon.webtest.tags;
import java.text.simpledateformat;
import java.util.date;
import javax.servlet..jspexception;
import javax.servlet.jsp.jspwriter;
import javax.servlet.jsp.tagext.tagsupport;
public class datetime extends tagsupport {
private static final long serialversionuid = 1l;
public int dostarttag() throws jspexception {
try {
jspwriter out = this.pagecontext.getout();
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
out.println("<span>");
out.println(sdf.format(new date()));
out.println("</span>");
} catch (exception e) {
throw new jspexception(e.getmessage());
}
return skip_body;
}
}
package leon.webtest.tags;
import java.text.simpledateformat;
import java.util.date;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.jspwriter;
import javax.servlet.jsp.tagext.tagsupport;
public class datetime extends tagsupport {
private static final long serialversionuid = 1l;
public int dostarttag() throws jspexception {
try {
jspwriter out = this.pagecontext.getout();
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
out.println("<span>");
out.println(sdf.format(new date()));
out.println("</span>");
} catch (exception e) {
throw new jspexception(e.getmessage());
}
return skip_body;
}
}
新建tld文件:web-inf/tld/webtest-tags.tld
[html] <?xml version="1.0" encoding="utf-8" ?>
<taglib xmlns="https://java.sun.com/xml/ns/j2ee"
xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
xsi:schemallocation="https://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>examples</short-name>
<uri>/mytags</uri>
<description>jsp self-define tag library.</description>
<tag>
<description>this is an date time tag</description>
<name>datetime</name>
<tag-class>leon.webtest.tags.datetime</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
<?xml version="1.0" encoding="utf-8" ?>
<taglib xmlns="https://java.sun.com/xml/ns/j2ee"
xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
xsi:schemallocation="https://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>examples</short-name>
<uri>/mytags</uri>
<description>jsp self-define tag library.</description>
<tag>
<description>this is an date time tag</description>
<name>datetime</name>
<tag-class>leon.webtest.tags.datetime</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
web.xml中的配置:
[html] <?xml version="1.0" encoding="utf-8"?>
<web-app id="webapp_id" version="2.4"
xmlns="https://java.sun.com/xml/ns/j2ee"
xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>webtest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/mytags</taglib-uri>
<taglib-location>/web-inf/tld/webtest-tag.tld
</taglib-location>
</taglib>
</jsp-config>
</web-app>
<?xml version="1.0" encoding="utf-8"?>
<web-app id="webapp_id" version="2.4"
xmlns="https://java.sun.com/xml/ns/j2ee"
xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>webtest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/mytags</taglib-uri>
<taglib-location>/web-inf/tld/webtest-tag.tld
</taglib-location>
</taglib>
</jsp-config>
</web-app>
jsp中的使用:
首先是导入:
<%@ taglib uri="/mytags" prefix="leon" %>
使用标签:
<leon:datetime />
加强版:需要显示出来的时间是动态的,像时钟一样会跳秒的。
上一篇: 提高Word2003打开长篇图文速度
下一篇: TIA发布新电信设施标准 支持云计算发展