1. Create a TLD (tag library descriptor) file namedmy-tags.tld
. For
more information, see Creating
a Tag Library Descriptor.
例如:
<taglib> <display-name>Raptor JSP Taglib</display-name> <tlib-version>1.0</tlib-version> <jspversion>2.0</jspversion> <short-name>r</short-name> <uri>http://ebay.com/raptor</uri> <tag> <name>deferredFragment</name> <tagclass>com.ebay.raptor.fragment.jsp.DeferredFragmentTag</tagclass> <body-content>scriptless</body-content> <!--dynamic-attributes>true</dynamic-attributes--> <attribute> <name>afterRender</name> <type>String</type> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>beforeRender</name> <type>String</type> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>dependency</name> <type>Object</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>fragmentDependencies</name> <type>String</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>delay</name> <type>int</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>timeoutFragment</name> <type>String</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>timeoutURL</name> <type>String</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>id</name> <type>String</type> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>inline</name> <type>boolean</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>outerElement</name> <type>String</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>style</name> <type>String</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>styleClass</name> <type>String</type> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
2. Create a directory containing the compiled Java tag handler class files used in your tag library.
3. Create
a sub-directory of the directory you created in step 2 and call it META-INF
.
注意my-tag.tld必须放在META-INF里,这也是web container回去search tld文件的位置
4. Copy the my-tags.tld
file
you created in step
1 into the META-INF
directory
you created in step
3
5. Archive your compiled Java class files into a jar
file
6.Copy the jar
file
into the WEB-INF/lib
directory of the Web application that uses your tag library.
有些文档说需要在web.xml 中提供对tag library 的引用才可以在JSP中使用,如下:
<taglib> <taglib-uri>myjar.tld</taglib-uri> <taglib-location> /WEB-INF/lib/myTagLibrary.jar </taglib-location> </taglib>
但实际情况是,至少在Tomcat 和 Geronimo 服务器中,不需要上面的配置,服务器可以自己找到Jar里的tld文件,可能不同的服务器其实现不一样吧。
7.Reference the tag library
in your JSP. For example:
<%@ taglib prefix="r" uri="http://ebay.com/raptor"%>
<r:includeRaptorJS />
参考:
http://docs.oracle.com/cd/E15051_01/wls/docs103/taglib/admin.html#wp364493
http://www.ibm.com/developerworks/cn/java/j-jsp07233/index.html