建立 HelloWorldSimpleTag 工程的详细步骤

用 Eclipse JEE 版本,建立一个空的 Dynamic Web Project。添加 Java 文件,添加标签描述文件,修改 web.xml 添加引用信息,再在具体的JSP文件中使用标签。最终这个工程的目录结构如附件的 hellotag.txt 文件,摘取必要如下:

├─src
│  └─hello
│          HelloWorldSimpleTag.java
└─WebContent
    │  hellotag.jsp
    └─WEB-INF
        │  web.xml
        ├─lib
        └─tags
                helloworld-taglib.xml (这个文件最好还是用 tld 後缀名,可以让容器自动加载)

下边就一步一步来。

添加 Java 文件

这个就是自写标签的功能部分了。代码如下:

package hello;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HelloWorldSimpleTag extends SimpleTagSupport {
    /*
     * 实际上调用标签就是通过调用这个方法实现的。
    */
    @Override
    public void doTag() throws JspException, IOException {
        this.getJspContext().getOut().write("Hello, world! JSP tag");
    }
}

此文件放置在 src/hello 目录下,系统自动编译後的 class 文件在 /WEB-INF/classes/hello 下。

添加标签描述文件

标签的功能部件写好了,总得要个东东描述一下吧,相当于说明一下 metadata,这个文件就是 /WEB-INF/tags/helloworld-taglib.xml,文件内容如下:

<?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>SimpleTagLibrary</short-name>
    <uri>/SimpleTagLibrary</uri>
    <tag>
        <description>Outputs Hello, World</description>
        <name>helloWorld</name>
        <tag-class>hello.HelloWorldSimpleTag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>

修改 web.xml 增添引用

要知道页面调用什么东东,都得靠这个 web.xml 来协调。那么标签写好了,标签的描述也写好了,总得给具体页面一个引用方式吧,好啊,吧 web.xml 改成下面的样子:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>hellotag</display-name>
  <welcome-file-list>
    <welcome-file>hellotag.jsp</welcome-file>
  </welcome-file-list>
  <jsp-config>
      <taglib>
          <taglib-uri>iridiumcao@gmail.com</taglib-uri>
          <taglib-location>/WEB-INF/tags/helloworld-taglib.xml</taglib-location>
      </taglib>
  </jsp-config>
</web-app>

使用标签

好了。准备工作完成——标签写好了,标签描述文件也写好了,web.xml 中的引用信息也添加了。开始使用吧。写一个页面 WebContent/hellotag.jsp 试试看,文件内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="iridium" uri="iridiumcao@gmail.com"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<iridium:helloWorld />
</body>
</html>

至此,这个工程就写完了。在 Eclipse 环境中,右键单击工程,Run As -> Run on Server,稍等片刻,就能看到“Insert title hereHello, world! JSP tag”的字样了。整个工程打包在附件的 hellotag.zip 文件中。