自定义一个很简单的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 />
加强版:需要显示出来的时间是动态的,像时钟一样会跳秒的。
上一篇: 老师,我有例假
下一篇: 安史之乱中,为何杨国忠会成为泄愤对象呢?
推荐阅读
-
一个实用的JSP分页代码
-
phpcms在自定义模块中的自定义标签分页,phpcms分页
-
如何在Java自定义标签中让自定义的标签属性正确接收EL表达式对象
-
如何在Java自定义标签中让自定义的标签属性正确接收EL表达式对象
-
自己开发的一个生成多级select的自定义标签
-
如何设计一个高效的自定义字段表单数据表?
-
问一个很简单的关于html代码的问题.标题要长!!!!!!!!_html/css_WEB-ITnose
-
JSP开发之JSP 标准标签库(JSTL)之JSTL函数split()函数&startsWith()函数的使用
-
一个可以防止刷新的JSP计数器
-
java 字符串相减(很简单的一个方法)