File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Setting up form data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Setting up form data" Watch "Setting up form data" New topic

Setting up form data

Gezza Hall
Ranch Hand

Joined: Jan 04, 2005
Posts: 33

Most frameworks offer some way of initializing your backing form object before the actual execute method is called. For example, you want some of your form fields to contain data before the form is presented to the user.

I've never known the best (simplest) way of doing this in struts. I did it once, but it was horribly complex code - there must be a standard way of doing this?

Any help appreciated!
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Struts has reset() method in ActionForm which executed every time you submit request through struts.
You can write the code to initialize any form attributes in this method.

However I am not sure, you this answers your question or not.

What are Backing form?


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Gezza Hall
Ranch Hand

Joined: Jan 04, 2005
Posts: 33
By Backing form, I mean your form backing object. So for example the form which is displayed to the user will have a text input field 'surname', and the form object will have a String surname, with the usual get and set methods. In this case I would want to set the field before the form is displayed (maybe from a user object in the session).

Not sure the reset() method is what I'm looking for?
Gezza Hall
Ranch Hand

Joined: Jan 04, 2005
Posts: 33
I think the fact that no one has answered this simple question demonstrates why I will always choose SpringMVC in future!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Gezza Hall:
... no one has answered this simple question ...

I think it's a bit presumptuous to assume that you'll get an answer right away if you post something when it's 6 PM Eastern time in the US and the work day is over for many of us. Don't forget that no one gets paid to answer your questions. We're happy to help when we can, but if someone doesn't get to your question right away, that's just the way it works in an "advice for free" forum.

Consultant, Sima Solutions
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

It's best to start with an overview of how Struts sets up an ActionForm (this list is quoted from Programming Jakarta Struts, 2nd Edition):
  • Check the mapping for the action and see if an ActionForm has been configured.
  • If an ActionForm is configured for the action, use the name attribute from the action element to look up the form bean configuration information.
  • Check to see if an instance of the ActionForm already has been created.
  • If an ActionForm instance is present in the appropriate scope and it's the same type as needed for the new request, reuse it.
  • Otherwise, create a new instance of the required ActionForm and store it in the appropriate scope (set by the scope attribute for the action element).
  • Call the reset() method on the ActionForm instance.
  • Iterate through the request parameters, and populate every parameter name that has a corresponding setter method in the ActionForm with the value for that request parameter.
  • Finally, if the validate attribute is set to "true", invoke the validate() method on the ActionForm instance and return any errors.

  • So if you need to pre-populate your formbean (ActionForm instance), you can do it in the constructor of the ActionForm.

    Where I normally do it, however, is in the default action for my Action class (I generally use DispatchAction). For example, our default is a method called "display". When this action is called, the formbean is created, and then the Action.display() is called. I do any work there to get data out of the session, etc.

    Once the action method is complete, Struts completes the action by forwarding to whatever path I've configured (e.g., the jsp for displaying the content).

    So Struts provides a very easy way to popluate your form that's part of the standard usage.
    [ April 25, 2008: Message edited by: Stevi Deter ]

    There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
    Gezza Hall
    Ranch Hand

    Joined: Jan 04, 2005
    Posts: 33
    Stevi, many thanks for your reply - and apologies to all if I was a little presumptuous in my earlier remark.

    Like you, I would not normally want to use the form constructor, as I would need various session attributes to work with.

    However, I'm not so sure what you mean by the default action - does struts just not simply call the execute method of the Action class? If it's possible to show any code snippets, or link to an example, the would be a great help.

    I agree. Here's the link:
    subject: Setting up form data
    It's not a secret anymore!