1、标记处理程序类
当定义一个新标记是,又要任务是定义一个java类,告诉系统当看见该标记是做什么。该类必须实现javax.sevlet.jsp.tagext.Tag接口,通过扩展TagSupport或BodyTagSupport类实现它,javax.sevlet.jsp.tagext.Tag在Tomcat安装路径中/common/lib,将jsp-api.jar文件解压就可以得到javax包。
package moreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
/* Very simple JSP tag that just inserts a string
* ("Custom tag example...") into the ourput.
* The actual name of the Tag Library Descriptor here;
* that is given by the Tag Library Desriptor (TLD)
* fiel that is referenced by the taglib directive
* in the JSP file.
*/
public class ExampleTag extends TagSupport
{
public int doStartTag()
{
try
{
JspWriter out=pageContext.getOut();
out.print("A simple example of tag,the class name is "+
"(moreservlets.tags.ExampleTag)");
}catch(IOException ioe)
{
System.out.println("Error in ExampleTag: "+ioe);
}
return(SKIP_BODY);
}
}
程序清单1-1
程序清单1-1是一个简单标记的例子,他只将A simple example of tag,the class name is (moreservlets.tags.ExampleTag)插入到jsp页中使用该标记的地方。记住:该类在moreservlets.tags包中,类的名称是Exampletag。
2、标记库描述符文件
一旦定义了标记处理成簇,下一个任务是向服务器表示该类并将它与特定的xml标记命相联,该任务统购标记库描述符文件(xml格式)来完成,如程序清单1-2所示。该文件包含一些特定的信息、一个库的短名、一个短描述以及一系列标记描述。
msajsp-taglib.tld:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>msajsp-tags</short-name>
<description>
A simple tab for the examples
</description>
<tag>
<name>example</name>
<tag-class>moreservlets.tags.ExampleTag</tag-class>
<body-content>empty</body-content>
<description> Output some strings </description>
</tag>
</taglib>
程序清单1-2
程序清单中的非粗体部分对所有的编辑描述符文件多是相同的,不要手工输入这个模板,可以从服务器缺省的xml文件中拷贝。在这里我们只要知道tag元素定义标记的主名(即标记的后缀)并标识处理该标记的类即可。由于标记处理程序类在moreservlets.tags包中,因此可使用moreservlets.tags.Exampletag的全限定类名。
3、jsp文件
一旦有了一个标记处理成簇的实现和一个标记库描述,就可以开始准备编写使用此标记的jsp文件了。见程序清单1-3,在首次使用标记前的某个地方,需要使用taglib指令:改指令有以下格式:
<%@ taglib uri = “…” prefix = “…” %>
所需的uri属性是一个引用标记库描述符文件的绝对或相对url,如程序清单1-2。prefix属性,该属性制定一个将要在标记库描述符文件定义的标记命前面使用的前缀。例如,如果tld文件定义了一个名为tag1的标记,并且prefix属性的值为test,那么实际的站;将为test:tag1>该标记可按以下两种方法使用,日过他被定义为一个利用该标记体的容器,则使用: <test:tag1> Arbitrary jsp </test:tag1>
否则使用:<test:tag1 />
SimpleTag.jsp:
<!DOCTYPE html PUBLIC "-//W 3C //DTD XHTML 4.0 Transitional//EN">
<html>
<head>
<%@ taglib uri="msajsp-taglib.tld" prefix="msajsp" %>
<title><msajsp:example /></title>
<LINK REL=STYLESHEET
HREF="JSP-Style.css"
TYPE="text/css">
</head>
<body>
<H1><msajsp:example /></H1>
<msajsp:example />
</body>
</html>
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<taglib>
<taglib-uri>
msajsp-taglib.tld
</taglib-uri>
<taglib-location>
/WEB-INF/msajsp-taglib.tld
</taglib-location>
</taglib>
</web-app>
程序清单1-4
各个文件的位置:1、我们在tomcat的webapps文件夹中建立一个名字为simpletag的文件夹;2、在simpletag文件夹中建立一个WEB-INF文件夹,在WEB-INF 文件夹中建 立一个classes文件夹,将已编译标记处理程序类包文件拷贝到classes文件夹中;3、将SimpleTag.jsp的网页拷贝到simpletag中,将msajsp-taglib.tld 和Web.xml和文件拷贝到WEB-INF文件夹中。
关系图解:
新启动tomcat服务器,在浏览器的地址栏中输入:http://localhost/simpletag/SimpleTag.jsp即可。