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

Prepopulating a form

ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Its very recently that I have begun playing around with struts and I have been trying to get my head around this for sometime now and I am almost begining to think that its not possible.

What I want to do is pre-populate a Struts form and then call a jsp which uses the struts-el tags to display the form.

I have a form with 2 fields - a select field and a radio button field. The select field has a list of 'network' options the user can select from and the radio button field has 2 views that the user can again choose from.

I have 2 urls (/selectNetwork and /submitNetwork) - one which displays the form and the other which the form is submitted to. This is what I have come up with.


In the form view url's (/networkSelect) Action class, I manually create a NetworkForm object, populate the attributes and set it in request scope with the same name (networkForm) as defined in the form bean definitions in the xml above. The jsp picks up the form and displays the values correctly.

However in the submit url (/submitNetwork) Action class, the ActionForm passed into the execute method is null. I would expect that the framework would reuse the NetworkForm object (or create a new one) and populate the fields from the request. Is this because I manually created the form object in the previous request and botched up the framework processing (though I cant fugure out why it should be so)?

Alternatively could I have included the form in the 'view' url too and set the scope to session? I dislike having to do so since this really is not a session object and is necessary only for 2 requests. Also if I do that, I am at a loss how to include validation in here. The problem is I dont want the validate method to kick in for the view (which is another reason why I dint map the form object to the view url).

I appreciate your patience in reading this post and would be grateful for any suggestions/comments.

vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi ramprasad

All Action tags should come inside a single action-mappings tag
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I am not sure if the duplicate action-mappings tags are causing the problem. It seem like if you are able to call your action and NetworkSubmitHandler is being called then that might not be the issue. I am not sure what else would cause this. You have a name set for the action submitNetwork action, so Struts should create a new instance of your form and try to populate it with values from the request.

About validation: By default validation is turned on for all actions, but if you set validate to false for the action networkSelect, then your form values will only be validated on the submitNetwork action. Your input property may need to point back to another action if you need to do some setup before the jsp is shown (like populating lists).

- Brent
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Thanks VidyaSagar and Brent.
Yes, the problem wasnt with the multiple action-mappings. In my earlier post, I had written that I manually created a Form object for prepopulation. I had made the default no args constructor required by the framework unavailable by defining my custom one - yes stupid me - I know .

Thanks for your help.

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