This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting up form data

 
Gezza Hall
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

V
 
Gezza Hall
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    Gezza Hall
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Thanks.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic