Recipe2.2.Eliminating Tag Library Declarations


Recipe 2.2. Eliminating Tag Library Declarations

Problem

You want to avoid having to add taglib elements to the web.xml file every time you want to use a new tag library.

Solution

Create a JSP file containing taglib directives that refer to the absolute URIs for the tag libraries you are using. Example 2-4 (taglibs.inc.jsp) shows a JSP file containing the taglib declarations for the Struts bean, html, and logic tag libraries as well as the JSTL core and formatting tag libraries.

Example 2-4. Common tag library declarations
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>

Then include this file in all of your JSP pages using the include directive:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!-- start taglib --> <%@ include file="/includes/taglibs.inc.jsp" %> <!-- end taglib --> <html:html>   <body>     ...

Since you are using the absolute URIs in the taglib directive, you aren't required to enter a corresponding taglib element in the application's web.xml file.

Discussion

If you are using a JSP 1.2/Servlet 2.3 compliant container, such as Tomcat 4.x or later, you can use an absolute URI in the taglib directive on the JSP page and you don't have to specify taglib elements in the web.xml.

Prior to the Servlet 2.3 specification, you were required to declare any JSP tag libraries you used in your applications web.xml deployment descriptor. The following snippet from a web.xml deployment descriptor shows the typical taglib descriptors used for a Struts application:

<!-- Struts Tag Library Descriptors --> <taglib>   <taglib-uri>/tags/struts-bean</taglib-uri>   <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/tags/struts-html</taglib-uri>   <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/tags/struts-logic</taglib-uri>   <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/tags/struts-nested</taglib-uri>   <taglib-location>/WEB-INF/struts-nested.tld</taglib-location> </taglib>

With the introduction of the Servlet 2.3 specification, a tag library's absolute URI is specified in that library's tag library descriptor (TLD) file. For example, here's this declaration from the struts-bean.tld file:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag  Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib>   <tlibversion>1.0</tlibversion>   <jspversion>1.1</jspversion>   <shortname>bean</shortname>    <uri>http://jakarta.apache.org/struts/tags-bean</uri>

Any JSP page that needs to use this tag library can reference it with the following page-level directive. The tag library doesn't need to be referenced in the web.xml file:

<%@ taglib         uri="http://jakarta.apache.org/struts/tags-bean"         prefix="bean" %>

If you use the same tag libraries throughout your application, you can use the approach shown in the Solution, which is creating an include file that contains all your needed taglib directives. No significant performance hit occurs if you refer to tag libraries that aren't needed, so you can safely include this file on every JSP page. If a URI changes, you will only need to change the one include file instead of every JSP.

Table 2-1 shows the complete list of tag library absolute URIs for Versions 1.1 and 1.2 of Struts.

Table 2-1. Struts tag library URIs

Tag library

Struts 1.1 URI

Struts 1.2 URI

struts-bean

http://jakarta.apache.org/struts/tags-bean

http://struts.apache.org/tags-bean

struts-html

http://jakarta.apache.org/struts/tags-html

http://struts.apache.org/tags-html

struts-logic

http://jakarta.apache.org/struts/tags-logic

http://struts.apache.org/tags-logic

struts-nested

http://jakarta.apache.org/struts/tags-nested

http://struts.apache.org/tags-nested

struts-template

http://jakarta.apache.org/struts/tags-template

No longer included with Struts; replaced by Tiles

struts-tiles

http://jakarta.apache.org/struts/tags-tiles

http://struts.apache.org/tags-tiles

struts-bean-el

http://jakarta.apache.org/struts/tags-bean-el

http://struts.apache.org/tags-bean-el

struts-html-el

http://jakarta.apache.org/struts/tags-html-el

http://struts.apache.org/tags-html-el

struts-logic-el

http://jakarta.apache.org/struts/tags-logic-el

http://struts.apache.org/tags-logic-el


Some developers (like me) enjoy using the absolute URIs, others still prefer to make the declarations in the web.xml. They make the point that using the latter approach ensures that your application code is shielded from underlying changes to the tag libraries. If a URI changes, you only need to change the web.xml file which is a deployment descriptor. You don't have to make changes to any JSP pages, even included JSP fragments. This argument is valid and has merit. In the end, it primarily comes down to personal preference.

See Also

The JavaServer Pages specification has some fairly complicated rules that it follows to resolve tag library URIs. Complete details can be found in the JSP specifications which can be downloaded from http://java.sun.com/jsp. Recipe 3.1 shows how to use the JSP Standard Tag Library (JSTL) tags within your Struts application.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net