Question

I am developing a Struts based application. I am new to Struts. I want to use html tags , specified in a taglib directory provided by Struts, in a JSP page.

But don't know how to use it. I know how to use taglib directive but I came to know from sources that the .tld file has been embedded in a .jar file after version 1.2.8. I am using Struts version 2.1.8.

I don't know in which .jar file the struts-html.tld file is located.

Was it helpful?

Solution

I'm using Struts 1.3.10 for this illustration:

  1. Download the latest struts library here (http://struts.apache.org/download.cgi#struts1310). Remember, the Full Distribution is that what you have to download as it contains a war file with the Struts TLD's.
  2. On your web application, copy all the lib in the archive file you downloaded to your /WEB-INF/lib folder.
  3. For JSTL libraries (which works well with struts) go here (http://java.sun.com/products/jsp/jstl/)
  4. Once you have your Struts TLD's and JSTL Tld's, put them under the /WEB-INF/tld/ folder (it must be situated in the /WEB-INF/ folder).
  5. On web.xml add the following stuff (under the <web-app> element)

      <jsp-config>
        <taglib>
            <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/sslext.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/sslext.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/struts-layout.tld</taglib-uri>
            <taglib-location>/WEB-INF/tld/struts-layout.tld</taglib-location>
        </taglib>
    
        <!-- Sun's JSTL -->
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/fn</taglib-uri>
            <taglib-location>/WEB-INF/tld/fn.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/fmt-1-0</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt-1_0.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt-rt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/fmt-1-0-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt-1_0-rt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
            <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/core-1-0</taglib-uri>
            <taglib-location>/WEB-INF/tld/c-1_0.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/core-1-0-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/c-1_0-rt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
            <taglib-location>/WEB-INF/tld/sql.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/sql-1-0</taglib-uri>
            <taglib-location>/WEB-INF/tld/sql-1_0.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/sql-rt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/sql-1-0-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/sql-1_0-rt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
            <taglib-location>/WEB-INF/tld/x.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/x-1-0</taglib-uri>
            <taglib-location>/WEB-INF/tld/x-1_0.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/x-rt.tld</taglib-location>
        </taglib>
    
        <taglib>
            <taglib-uri>http://java.sun.com/jstl/x-1-0-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/x-1_0-rt.tld</taglib-location>
        </taglib>
    </jsp-config>
    

This tells that once you call your TLD from the JSP, your webapp will look for the matching <taglib-uri> then look for it's location on <taglib-location> and find relevant class to call.

On your JSP, now you can do this:

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-layout.tld" prefix="layout"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jstl/fn" prefix="fn" %>

Hope this helps.

OTHER TIPS

Even if this question has an (to me, incomprehensibly) accepted answer, I feel obliged to have a go at it myself. The real answer is contained in BalusC's comment and suggesting stray .tld files laying around in WEB-INF is really bad advice. My intention is expand on this using the exact version of Struts2 the OP was asking about (v2.1.8), which I downloaded from Apache's historical archive.

  1. I don't know in which .jar file the struts-html.tld file is located.

There is no struts-html.tld in Struts2 - Instead there are the following:

  • struts-tags.tld, which resides in the META-INF directory of struts2-core-2.1.8.jar and contains all standard Struts tags, like the ones you'd expect to find in struts-html.tld in Struts1.
  • tiles-jsp.tld, which resides in the META-INF directory of tiles-jsp-2.0.6.jar and corresponds to what was struts-tiles.tld in Struts1.
  • some more TLDs, e.g. for SiteMesh which are not directly related to the question.

  1. I want to use html tags, specified in a taglib directory provided by Struts, in a JSP page. But don't know how to use it. I know how to use taglib directive but I came to know from sources that the .tld file has been embedded in a .jar file after version 1.2.8.

That is correct. The way it generally works is the following:

  • When the servlet container starts up, it looks through the WEB-INF/lib directory and loads any .jar files it finds there - This is where you need to place the Struts2 library.
  • Inside these .jar files, any TLDs are expected to reside in the META-INF directory. Obviously and as mentioned above, this already is the case for struts2-core-2.1.8.jar, so there's nothing that needs to be done.
  • When the servlet container loads the TLD, it looks for the <uri> element inside the root element <taglib> and stores a mapping between that TLD and its URI. Correspondingly, this URI is used in your .jsp files to reference the TLD.

In the case of struts2-core-2.1.8.jar, the URI is /struts-tags and thus you need to reference it in a .jsp file like this (of course you can change the prefix attribute to your liking)...

<%@ taglib uri="/struts-tags" prefix="s" %>

...and subsequently put it to use, like e.g. this:

<s:form action="HelloWorld">
    (...)
</s:form>

BalusC is correct: you just need the JAR files in /WEB-INF/lib and a proper taglib in each JSP. One thing that tripped me up initially was the sensitive JSP syntax; spaces between some elements are important. Per the Stripes documentation, each taglib for the Stripes TLD should look like:

<%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top