On Feb 20, 2008 5:48 AM, Adam Hardy <[EMAIL PROTECTED]>
wrote:

> I just checked that there is no JSTL configuration setting to apply a
> format
> type or pattern to <fmt:formatDate> across the whole webapp.
>
> I need the ISO date format on 99% of dates displayed on my current app,
> but
> there is no way to change the default globally for <fmt:formatDate> from
> dateStyle="medium".
>
> It seems like a prime target for a quick taglib, to avoid using the
> following
> everywhere, with x number of errors by sloppy coders (that would be me):
>
> <fmt:formatDate value="myDate" pattern="yyyy-MM-dd" />
>
> Or is there something I'm missing? Somewhat off-topic I realise after
> checking
> the S2 taglib reference :O
>
> It's a bit counter intuitive, in my opinion, but I think the way Struts 2
does this type of formatting may be just what you need.  Instead of
providing a dedicated tag to do formatting, the <s:text> tag can be pressed
into service as a formatter.  Basically you need to set up a property file
and configure it in your struts.xml that will hold all the i18n properties
for your web site.  Something like:

<constant name="struts.custom.i18n.resources" value="application"/>

would direct struts to look for an application.properties file on the
classpath (probably in WEB-INF/classes).  Then in that file you need to
define the formats you care about.  Since you have a default format that you
want to use throughout your site, you might have something like:

format.date.default={0,date,yyyy-MM-dd}

Then in your .jsp pages you would simply use:

<s:text name="format.date.default"><s:param value="%{my.date.property
}"/></s:text>

Since the job of <s:text> is to look up strings from the properties and run
them through java.text.MessageFormat, passing in any supplied parameters,
the output is formatted as a date in the format you want.  You can have as
many different formats as you need, and you can format anything that
MessageFormat can format (dates, times, numbers).  Hope that helps.
  (*Chris*)

Reply via email to