Tuesday, March 1, 2011

Format Date with fmt:formatDate JSP

I am trying to format a date with:

<fmt:formatDate value="${newsletter.createdOn}" pattern="MM/dd/yyyy"/>

newsletter is an object with a createdOn property which is java.util.Date.

When I invoke the previous sentence I get:

According to the TLD, the attribute value does not accept expressions.

I am importing fmt with

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

Does anyone know how can I work around this problem?

I am using the jstl.jar coming with tomcat.

Under jstl.jar/META-INF/MANIFEST.MF stays:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.5.3 
Created-By: 1.4.2-b28 (Sun Microsystems Inc.)
Specification-Title: JavaServer Pages Standard Tag Library (JSTL)
Specification-Version: 1.1
Implementation-Title: JavaServer Pages Standard Tag Library API Refere
 nce Implementation
Implementation-Version: 1.1.0-D13
Implementation-Vendor: Sun Microsystems, Inc.
Implementation-Vendor-Id: com.sun
Extension-Name: javax.servlet.jsp.jstl

I am using Apache Tomcat Version 6.0.14

From stackoverflow
  • Are you sure you're not using the runtime versions of the tag lib? May we see the library import statement?

    I think lucus is onto something, according to this FAQ on JavaRanch, that's a JSTL 1.0 declaration. You might want to update to 1.1.

    What's your environment, app server, and version?

    Sergio del Amo : added in the main question
    sblundy : Do you know what version you're using?
    Sergio del Amo : updated the main question
  • Try

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
  • Are you using the fmt-1_0-rt.tld or fmt-1_0.tld taglib.

    The difference is the settings for rtexprvalue

    In one, this is false, in the other it is true.

    Sergio del Amo : No idea, how can i know this. I am using the jstl.jar where this tags are defined.
  • Are you using JSTL 1.0 or 1.1? formatDate in 1.1 should accept expressions.

    Sergio del Amo : i think jstl 1.1
  • Apparently, i needed 1.1 but i had to change the library import statements for both c and fmt.
    Now it works. Thanks for the help, and sorry for the confusion.

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
  • This guy seems to have worked around the problem by extracting the TLD from the jar, modifying it, placing it in the WAR's WEB-INF directory, and adding an entry to his web.xml like this:

    <jsp-config>
     <taglib>
      <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
      <taglib-location>/WEB-INF/fmt.tld</taglib-location>
     </taglib>
    </jsp-config>
    

    In the end, he switched to the 1.1 declaration:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"/>
    
    Sergio del Amo : problem is solved sblundy. Thanks for your help. You rock!

0 comments:

Post a Comment