java:jsp: 一个简单的自定义标签 tld

时间:2021-10-09 22:57:59

java:jsp: 一个简单的自定义标签 tld

请注意,uri都是:http://www.tag.com/mytag,保持统一,要不然报错,不能访问

tld文件

 <?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>mytag 1.1 print library</description>
<display-name>mytag 标签库</display-name>
<tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<uri>http://www.tag.com/mytag</uri>
<tag>
<description>打印 Hello</description>
<name>print</name>
<tag-class>cn.tag.MytagPrint</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>

在web.xml文件中加入jsp-config配置,如果报错,请将web.xml头部的"<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >"删除掉:

 <web-app>
<display-name>Archetype Created Web Application</display-name> <resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> <jsp-config>
<taglib>
<taglib-uri>http://www.tag.com/mytag</taglib-uri>
<taglib-location>/WEB-INF/mytlds/mytag.tld</taglib-location>
</taglib>
</jsp-config> </web-app>

tag.jsp

 <%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="mytag" uri="http://www.tag.com/mytag" %>
<html>
<head><title>简单标签实例</title></head>
<body> <h3>调用 mytag 标签库中的 print 标签</h3>
调用 print 标签的结果:<mytag:print /> </body>
</html>