• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Setting up form data

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic