[jsp自定义标签1] jsp自定义标签的处理过程
jsp自定义标签可以封装一定的功能,在某种层面上实现数据和展示的分离。
jsp自定义标签的主要工作包括:
1.创建标签的处理类(Tag Handler Class)
2.创建标签库描述文件(Tag Library Descriptor File)
3.在web.xml文件中配置元素
4.在JSP文件中引人标签库
下面给出一个简单的例子:
1.tag handler class
package com.fox.mytag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;public class HelloMyTag extends TagSupport {@Overridepublic int doEndTag() throws JspException {JspWriter out = pageContext.getOut();try {out.print("do End Tag ");} catch (IOException e) {e.printStackTrace();}return super.doEndTag();}@Overridepublic int doStartTag() throws JspException {JspWriter out = pageContext.getOut();try {out.print("<font color="red">hello!</font>");} catch (IOException e) {e.printStackTrace();}return super.doStartTag();}}<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"> <tlib-version>1.0</tlib-version> <short-name>myTagLib</short-name> <uri>/myTag</uri> <tag> <name>helloTag</name> <tag-class>com.fox.mytag.HelloMyTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
<jsp-config> <taglib> <taglib-uri>/myTag</taglib-uri> <taglib-location>/WEB-INF/tag/mytag.tld</taglib-location> </taglib> </jsp-config>