java:jsp: 一個簡單的自定義標簽 tld
阿新 • • 發佈:2018-01-10
isp javax pos web.xml systems xsd head sys Coding
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>
java:jsp: 一個簡單的自定義標簽 tld