Hi Sasha,

An alternative approach would be to create a generic mechanism to
populate the ActionForm with properly formatted string values. The idea
here is to write a method that transfers values from a bean to an
ActionForm, automatically applying default type conversion and
formatting based on the bean's property types. Another method can be
added to provide a way to override the default formatting behavior.

I write about this subject extensively in my book Jakarta Pitfalls
(Wiley). Chapter 2, "ActionForms", begins with this problem and its
solution. Later sections in the same chapter show how to extend the
mechanism to automatically validate inbound request values (without the
need for declarative specification), and then populate the JavaBean
with converted values using the inverse of the formatting scheme.

The value of this approach is that you don't have to encode the
formatting logic in your JSPs at all. All the formatting code is in one
place, and new JSPs automatically get the formatting behavior without
adding any new code. It helps ensure consistency too, especially if you
have a lot of pages/developers.

A free, downloadable implementation is provided in the example
solutions for Chapter 2, at
http://www.wiley.com/compbooks/dudney/jakarta/index.html under the
Chapters 2,3,4 link.

I'm a bit puzzled that you say that you can't get the user's locale in
your ActionForm. I would think you should be able to do something like
this in your Action to get the locale and set it on the form instance:

public ActionForward execute(...) {
    ...
    Locale currLocale = (Locale) 
        request.getSession().getAttribute(Globals.LOCALE_KEY);
    form.setLocale(currLocale);
    ...
}

Jonathan


> -----Original Message-----
> From: Sasha Borodin [mailto:[EMAIL PROTECTED]
> Sent: August 27, 2003 8:43 AM
> To: Struts Users Mailing List
> Subject: Formatting form data inside the *view* (JSP's)
>
>
> Thought I'd throw this out in the middle of the week again...
>
> Kind of a conceptual question:  how do you format Dates, Numbers,
> Currencies, etc. for *form fields* inside the JSP?
>
> I know I could do in in the ActionForm getters, but:
>     1.  I don't know the locate of the user at that point
>     2.  I'd have to recompile the ActionForm every time I want to 
make a
> change to the *view* - seems like a bad practice.
>
> What I'm looking to achieve is something similar to <fmt:format> JSTL

tag,
> where you can specify a format mask, or even a format "style" defined
> elsewhere.
>
> Thanks,
>
> -Sasha
>
> On 8/22/03 12:30, "Sasha Borodin" <[EMAIL PROTECTED]> wrote:
>
> > I would like to state a basic assumption I deduced in learning
> struts, then
> > pose a question.
> >
> > Assumption:  ActionForm property getters and setters should
> return and take
> > Strings.
> >
> > My initial impulse was to try passing the actual data types
> (Dates, Numbers,
> > etc.); however, the Struts mechanism seems to pass Strings to
> the ActionForm
> > setter methods on form submission (which throws a conversion
> Exception of
> > course). Then I tried making the setters take Strings, and
> getters return
> > the actual data types, but this seemed to confuse the introspection
> > mechanism; plus I didn't see any way to apply formatting inside the
> > html:text tags (like you can in JSTL's fmt:format tags).
> >
> > Question:  How does one handle formatting data (custom
> formatting or i18n)
> > for (pre/re)population of a form?
> >
> > Ex.:  An update form; a user's information is loaded from the
> database into
> > classes (Strings, Dates, Numbers, etc.).  If you want to format
> this data,
> > you have to do it in the Model, as the ActionForm only takes and 
gives
> > Strings - which seems to be a Bad Practice.  Even if you had 
additional
> > special setters for the explicit purpose of populating the
> ActionForm from
> > original data types, you still don't know what Locale the
> client is from.
> >
> > Is my assumption incorrect?  Is there a basic flaw in my
> understanding of
> > the use of ActionForm?  Or there an actual short-coming, and 
workaround?
> >
> > In short, How Can You Format Data For Strut's Form Tags Inside
> The Actual
> > JSP :-) ?
> >
> > Thank you for any input.
> >
> > -Sasha

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to