Format Date with fmt:formatDate JSP
Asked Answered
F

9

33

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

Fideliafidelio answered 11/11, 2008 at 16:26 Comment(5)
Formatting your date as "MM/dd/yyyy" is evil :) Use MMM dd yyyy instead, where MMM is the name of the month. Otherwise a date of say 1/6/2009 will leave have of your potential audience thinking it was published on the 1st June.Tryck
Do you know what version fo Tomcat you're running?Expressive
Ok, I'm confused. Have you done a clean build, shutdown, deleted %TOMCAT_HOME%\work, restarted and redeployed to make sure nothing's sticking around?Expressive
A clean build, shutdown, deleted %TOMCAT_HOME%\work, restarted and redeployed does not Help.Fideliafidelio
You didn't switch back to the 1.0 declaration, did you?Expressive
H
41

Try

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Halfsole answered 11/11, 2008 at 16:37 Comment(1)
The difference is the .../jsp/... part of the uri.Haroun
E
5

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"/>
Expressive answered 11/11, 2008 at 17:27 Comment(2)
Actually, only fixing the taglib URI was sufficient. Hassling with loose TLDs is a big myth and should really not be done.Subzero
The "This guy" link is brokenForsterite
B
2

I encountered the same issue.

I Changed <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> to <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %> and it worked for me!

Blandish answered 3/5, 2012 at 7:14 Comment(0)
E
1

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?

Expressive answered 11/11, 2008 at 16:33 Comment(1)
Do you know what version you're using?Expressive
S
1

Are you using JSTL 1.0 or 1.1? formatDate in 1.1 should accept expressions.

Smithery answered 11/11, 2008 at 16:39 Comment(0)
F
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" %>
Fideliafidelio answered 11/11, 2008 at 17:23 Comment(0)
G
1

It just worked for me by adding "_rt" to taglib url like this:

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

I found it in this article

have fun!

Glutamine answered 23/2, 2011 at 8:44 Comment(1)
You have an outdated JSTL version (that article is also old). Upgrade JSTL. Read our JSTL wiki page.Subzero
M
0

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.

Middle answered 11/11, 2008 at 16:38 Comment(1)
No idea, how can i know this. I am using the jstl.jar where this tags are defined.Fideliafidelio
S
0
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix='fmt'%>

is working for me.

Sophia answered 3/7, 2014 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.