Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declare JSP taglib directives in web.xml

Tags:

java

jsp

jsp-tags

I seem to remember reading that it's possible to declare taglib directives such as:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

in web.xml. This eliminates the need to duplicate this directive in every JSP file where the taglib is used. Could someone tell me how these directives can be added to web.xml?

like image 625
Dónal Avatar asked Oct 22 '08 16:10

Dónal


People also ask

What is Taglib in Web XML?

If you want to redistribute your tag files or implement your custom tags with tag handlers written in Java, you must declare the tags in a tag library descriptor (TLD). A tag library descriptor is an XML document that contains information about a library as a whole and about each tag contained in the library.

What is JSP Taglib directive?

The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides means for identifying the custom tags in your JSP page.

Where do I put Taglib in JSP?

Click the JSP Tags tab. In the Tag type drop-down list, select JSP Directive - taglib then click the Add button. On the Tag Library page of the dialog box, select one of the available tag libraries, or click the Import button to locate and add a tag library to the list, and then select it.

What is JSP Taglib at least 5?

JSTL provides tag libraries that include a wide range of actions to perform common tasks. For example, if you want to access data from database, you can use SQL tag library in your applications. JSTL is a standard tag library that is composed of five tag libraries.


2 Answers

The taglib element in web.xml serves a different purpose to the taglib directive which you have above.

As David said, the taglib directive is required on each page.

If you have many pages which use common taglibs, you can shortcut this by putting the taglib directives into an include file, and including this file each page. But no matter how you do it, the taglib directive has to be on the page somehow.

That tag you need to include on each page looks like this:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

If you have a custom taglib in a custom location, you can also specify a location relative to the webapp root:

 <%@ taglib prefix="ex" uri="/taglib.tld" %>

Further reading on the taglib directive

The taglib directive from web.xml maps tag uris to the physical location of your taglib. It is optional since JSP 2.0, as compliant containers will look in a set of standard locations to try to auto-discover the taglib: /WEB-INF and its subdirectories, /META-INF as well for JAR files.

It looks like this, in web.xml:

<taglib>
  <taglib-uri>
    http://www.example.com/taglib
  </taglib-uri>
  <taglib-location>
    /taglib.tld
  </taglib-location>
</taglib>

And the taglib is referenced in the JSP page like this (the taglib directive on each page is unavoidable!):

<%@ taglib prefix="ex" uri="http://www.example.com/taglib" %>

This is equivalent to the second example I gave for the taglib directive above. The biggest difference is in how you point to the taglib location.

This page contains a bit more information.

like image 153
Athena Avatar answered Sep 17 '22 12:09

Athena


Sorry, you're slightly mistaken. If a page uses a taglib, you have to have a taglib directive for it on the page. You could place the common taglib directives in an include file that all of your pages include with an include directive, but at compile time the taglib directive has to be there.

I prefer to NOT have the taglib elements in the web.xml, and instead have the taglib directive specify the URI value that is used in the "uri" element in the TLD that is inside the taglib jar file in your WEB-INF/lib.

like image 28
David M. Karr Avatar answered Sep 18 '22 12:09

David M. Karr