Whenever you call an action via action mapping, the standard create/reset/populate/validate sequence is fired. You can avoid it by not associating OtherForm with OtherAction, but in this case Struts will not automatically populate OtherForm if you submit request from browser to OtherAction.
Since you created OtherForm and put it in proper context with proper name, Struts does not recreate it, but calls reset() nevertheless (I hate when it does stuff for me automatically, but this is how it works.) It is up to you what to do in reset(). You do not have to reset the form. If you call OtherAction (1) directly from browser or (2) from SomeAction, you can use a token in request scope, that you initialize in SomeAction. If OtherForm().reset() finds token in the request, this is a chained call so you do nothing in reset(), otherwise you clear the form. Also, if you use request scope, you do not really need to clear the form, it is created with every request. If you use OtherForm for output only, then you can skip the setters, and initialize the form in constructor or directly accessing the fields, or using setters with different names. Struts is honest in this regard, it uses setters only to populate the form. No setters, no changes to the form. The best way to avoid these problems is not to chain actions ;-) Michael. On 6/7/06, Scott Van Wart <[EMAIL PROTECTED]> wrote:
Hi All, I'm having trouble forwarding one action to another and sending properties on through. Let's say I have two actions, "SomeAction" and "SomeOtherAction", and two form beans, "SomeForm" (with property "someProp") and "SomeOtherForm" (with property "someOtherProp"). If I want one action to go to the other, here's how I do it: ---[ struts-config.xml ]--- <form-beans> <form-bean name="someForm" type="mypkg.SomeForm" /> <form-bean name="someOtherForm" type="mypkg.SomeOtherForm" /> </form-beans> <action path="/someAction" type="mypkg.SomeAction" name="someForm" scope="request" validate="false"> <forward name="success" path="/someOtherAction.do" /> </action> <action path="/someOtherAction" type="mypkg.SomeOtherAction" name="someOtherForm" scope="request" validate="false"> <forward name="success" path="page.main" /> <forward name="failure" path="somewhere.bad" /> </action> --------------------------- Having fun so far? Good! ---[ SomeAction.java ]--- public class SomeAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception { SomeForm theForm = (SomeForm)form; SomeOtherForm newForm = new SomeOtherForm(); newForm.setSomeOtherProp( theForm.getSomeProp() ); request.setAttribute( "someOtherForm", newForm ); return mapping.findForward( "success" ); } } ------------------------- Almost done... ---[ SomeOtherAction.java ]--- public class SomeOtherAction extends Action { public Washroom execute( ... ) throws AFit { SomeOtherForm theForm = (SomeOtherForm)form; if ( theForm.getSomeOtherProp().equals( "" ) ) { return mapping.findForward( "failure" ); } else { return mapping.findForward( "success" ); } } } ----------------------------- The "failure" forward always gets hit because someOtherProp is always null. The form gets reset() after I prepopulate someOtherForm and loses the value I set! So I suspect the typical response to this might be "well, duh," but this is how I've taken to passing parameters across actions. Looks like I've been fooling myself with my other Action classes because most of the properties have the same name, so the reset() doesn't matter! My questions are: 1. Am I chaining the actions together properly (with the path="/blah.do"?) 2. Is this the appropriate way to prepopulate ActionForms? 3. Did anyone see my action throw a fit in the public washroom? Thanks, Scott (It's been a long day, I suspect my silliness in this public post will come back to haunt me in a few decades) --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]