java:jsp: 一个简单的自定义标签 tld
java:jsp: 一个简单的自定义标签 tld
请注意,uri都是:http://www.tag.com/mytag,保持统一,要不然报错,不能访问
tld文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 6 version="2.0"> 7 <description>mytag 1.1 print library</description> 8 <display-name>mytag 标签库</display-name> 9 <tlib-version>1.0</tlib-version> 10 <short-name>mytag</short-name> 11 <uri>http://www.tag.com/mytag</uri> 12 <tag> 13 <description>打印 Hello</description> 14 <name>print</name> 15 <tag-class>cn.tag.MytagPrint</tag-class> 16 <body-content>empty</body-content> 17 </tag> 18 </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" >"删除掉:
1 <web-app> 2 <display-name>Archetype Created Web Application</display-name> 3 4 <resource-ref> 5 <description>DB Connection</description> 6 <res-ref-name>jdbc/test</res-ref-name> 7 <res-type>javax.sql.DataSource</res-type> 8 <res-auth>Container</res-auth> 9 </resource-ref> 10 11 12 <jsp-config> 13 <taglib> 14 <taglib-uri>http://www.tag.com/mytag</taglib-uri> 15 <taglib-location>/WEB-INF/mytlds/mytag.tld</taglib-location> 16 </taglib> 17 </jsp-config> 18 19 20 </web-app>
tag.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8"%> 2 <%@ taglib prefix="mytag" uri="http://www.tag.com/mytag" %> 3 <html> 4 <head><title>简单标签实例</title></head> 5 <body> 6 7 <h3>调用 mytag 标签库中的 print 标签</h3> 8 调用 print 标签的结果:<mytag:print /> 9 10 </body> 11 </html>