jQuery in Action, 3rd edition
The moose likes Struts and the fly likes Bypassing ActionForm by using a query string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Bypassing ActionForm by using a query string" Watch "Bypassing ActionForm by using a query string" New topic

Bypassing ActionForm by using a query string

Tim Eyre

Joined: May 20, 2002
Posts: 20
In certain situations I want to get an ActionServlet to populate an action form and then forward to another ActionServlet. The second ActionServlet MUST have an associated ActionForm because users are able to populate it from an HTML form in the usual manner.
Can I populate the ActionForm and put it as an attribute in session/request scope so that it will be passed into my perform() method?
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Yes, I do this routinely. Another example is a pattern like the following:
1. user logs in
2. user accesses form for providing info they've
never given before (so all form fields are
3. user submits form
4. user logs out
5. user logs in
6. user accesses previous form, but it is
pre-populated with values from the last visit
You simply do the following:
MyForm frm=new MyForm();
// set frm values here ....
return mapping.forward("forward-name");
replacing "form-bean-name" with the value of the name attribute for your form bean in struts-config.xml (the one that has the same type as "MyForm" shown above). Use the appropriate "forward-name" to forward processing to the action that has the name attribute with the value matching your form bean.

Reid - SCJP2 (April 2002)
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6496

Ok, I guess you could but should you? I tend to shy away from this practice just because it's another point of duplication: if you ever change the form name in struts-config, you are going to have to remember to change the setAttribute() parameter value wherever you do this. To me, there's a high likelihood that this will be overlooked by a maintenance programmer. Also, it will not be very easy to debug because it probably won't cause a RuntimeException if the names get out of synch.
Since a form can be associated with multiple actions, I would just associate the first action with the same form that the second action requires. Might have to add a flag field in the form to indicate normal use (values taken from request) vs. initial use (values assigned by action). The flag can be implemented in several ways but my first cut would be a hidden field in one of the JSPs and a method in the form that checks for the presence of a value in that field to indicate normal use. The reset method would then be implemented to reset the flag field and other fields that you want to have "cleared".
When the first action is invoked, it receives a request with a blank form (since reset is called on the form before it is passed to the perform/execute method). The action would then query the flag on the form and prepopulate it accordingly, then forward the request to the second action. This approach will eliminate the problem I mentioned above and I'm pretty sure I've used it before if not recently.
edit: (doing a lot of refactoring in my head here )
Of course, the above approach wouldn't work if the first action was processing another type of form. In that case, I would probably just implement the reset in the form then check the flag and prepopulate accordingly in the second action's perform/execute. Come to think of it, I'd probably end up refactoring the approach above to this second approach.
Sorry if I'm rambling--woke up in the middle of the night with some reflux and I'm trying to get my stomach to settle down, maybe the acid is fogging up my thinking as well
[ January 31, 2003: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Re: reflux, tagamat before bed, or a generic equivalent helps (with the generics you'll probably find you'll need to take Rolaids as well). Cutting down on chocolate is supposed to help too. Be cautious of "the purple pill", there are some downsides to it.
Re: the renaming the form, I see the point, although I can't say I've actually seen it happen. What I tend to run into are changes in the page navigation, so the forwarding mechanisms change. The issue could be rendered moot in a couple of ways:
1) have unit tests that know the pre and post conditions for an action (a good idea for maintenance anyways)
2) get the name of the form at runtime from the ActionFormBean or more likely from FormBeanConfig.
I haven't tried #2 myself, but from a quick browse through the JavaDoc it looks like you would somehow have to create an appropriate ApplicationConfig instance for your app.
I agree. Here's the link: http://aspose.com/file-tools
subject: Bypassing ActionForm by using a query string
It's not a secret anymore!