January 12, 2006 at 8:28 pm #244505
NOTE:For J2EE 1.4 and later projects, due to a change in the format of the web.xml file all <taglib> entries in your web.xml file must be enclosed in a top-level <jsp-config></jsp-config> tag, as follows:
<jsp-config> <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/spring.tld</taglib-location> </taglib> </jsp-config>
This problem comes up alot in the forums and it stems from the fact that many people don’t quite understand what the purpose of the “uri” attribute in the <%@taglib %> tag is for. The URI is a Universal Resource Identifier, it is NOT a locator (URL). This means that the URI is used to uniquely identify each taglib in an internal registry of taglibs, much like a key is used to set/get values from a HashMap or Hashtable in Java.
According to the web application spec from Sun, resolving the URIs to actual tag libraries that can be loaded/called by the application takes place in the following order:
1. Check the web.xml file for matching taglib tags that have the given URI in them and then follow the taglib-location tag to actually load the TLD
2. If no match with #1 was found, recursively check the META-INF directory of all the JARs in the application for TLDs that contain the URI that was specified
Given this information you can start to see that the entering something like “/WEB-INF/struts-bean.html” in your URI string does not point the validator at the file in your WEB-INF directory, it simply uses that URI as a key to go lookup where the tag library actually is. You could just as easily put “/hamsandwich/with/cheese” in the URI field, and as long as you had that defined in your web.xml file to point to your struts-bean.tld file, it would work!
Now, most tag libraries (e.g. Struts) will ship their taglibs with default URIs already assigned to each library that you can use without mapping the TLD to a URI in the web.xml file. Struts 1.1 for example ships with the default URI for its bean library:
Without ever changing a line of code in your web.xml file, you can start using this URI in your JSP pages, and taglib resolution will work perfectly. However, if your tag library does not ship with default URIs or you wish to map the tags to something else, then you need to add taglib entries in your web.xml file that would look something like this:
<taglib> <taglib-uri>/tags/struts-beans</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib>
Now in your JSP page you would have something like this instead of the default URI:
<%@ taglib uri="/tags/struts-beans" prefix="bean" %>
As long as you are consistent, the taglib uri in the web.xml file and your JSP page can be whatever you want, you could just use “struts-bean” instead even if you wanted; but don’t forget that the URI must be unique in an entire system, that is why people tend to use long URLs as good URIs, for example “http://jakarta.apache.org/struts/tags-bean”, because the chances of overlap with another tag library using the same URI in the system is about 1 in a million. However if you just used the URI “bean”, your chances of overlap are quite high as many tag libraries exist to handle bean operations. One benefit of not using the default URI is that sometimes they change on you. For example between Struts 1.1 and 1.2, the default URIs for the Struts taglibs changed from what is shown above to: http://struts.apache.org/tags-bean due to Struts becoming a top level project at Apache. If you have been using your own mapped URIs the entire time, you may not need to change anything. However if you had been using the default URIs from Struts 1.1, then you would either need to go update all your JSP files to use the new default URIs, or you could add entries to your web.xml file that looked like this:
<taglib> <taglib-uri>http://jakarta.apache.org/struts/tags-bean</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib>
It looks strange because you are mapping the URI that used the be the default URI to a new file, but this might help you avoid updating 100s of JSPs in your project. We would suggest either sticking to default URIs per project if you know you won’t be updating the libs or mapping the libs right away to your own URIs like /tags/struts-bean or something along those lines.
- This topic was modified 4 years, 5 months ago by support-tony. Reason: Minor edits