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?
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
Joined: Jan 04, 2005
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?
Joined: Jan 04, 2005
I think the fact that no one has answered this simple question demonstrates why I will always choose SpringMVC in future!
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.
Gezza, 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
Joined: Jan 04, 2005
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.