You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Davide Bruzzone <Da...@ngt.com> on 2002/09/13 22:33:04 UTC
Struts - Passing a form object "through" an action
Greetings all...
I'm including a message that I posted on USENET a couple of days ago
below... Any help/insight/someone pointing out what a silly mistake I'm
making would be greatly appreciated:
Wayne wrote:
By default, a FormBean is session-scoped. That means it will be in the
session context under its form name. Just go look at it and get the
stuff you want.
Wayne
<interjection>
Yes, I can see the form bean in the session in my debugger... The values are
correct (i.e. They are the values from the previous page). They just never
make it back out to the page that's sent to the browser.
</interjection>
I wrote:
>Greetings all...
>
>Here's what we're trying to do: When an HTML form is posted to a
>Struts action, we'd like to take the form object that's passed as a
>parameter into the action method and somehow pass it back out to the
>view (in our case a JSP) that the action forwards the request to (So
>that the resulting view's values can be based on this form object's
>attributes).
>
>i.e...
>1) Form 1 has a field that contains the value "Foo"
>2) The form is submitted to an action that forwards the request to
>Form 2 (or back to Form 1). This second form also has a text field,
>and it should contain the value "Foo" when it (be it Form 2 or Form 1)
>is rendered.
>
>Here are some code snippets...
>
>- The JSP page:
>
><html:form action="/Test.do?action=view">
> <html:select property="sel">
> <%-- TestVector is the name of a Vector object in the
session --%>
> <html:options name="TestVector"/>
> </html:select>
>
> <input type="submit" value="Submit"/>
></html:form>
>
>- struts-config.xml:
>
><form-beans type="org.apache.struts.action.ActionFormBean">
> <form-bean name="TestForm" type="com.company.TestForm" />
></form-beans>
>
>...
>
><action name="TestForm" path="/Test"
> type="com.company.TestAction"
> scope="request"
> input="/test.jsp"
> parameter="action">
> <forward name="success" redirect="true" path="/test.jsp" />
></action>
>
>- The appropriate action class code:
>
>...
>
>if (form == null) {
> form = new TestForm();
>
> if ("request".equals(mapping.getScope()))
> request.setAttribute(mapping.getAttribute(), form);
> else
> session.setAttribute(mapping.getAttribute(), form);
>}
>
>TestForm testForm = (TestForm)form;
>
>testForm.setSomething("Two");
>
>- The appropriate form class code:
>
>private String something = null;
>
>public void setSomething(String newSomething) {
> something = newSomething;
>}
>
>public String getSomething() {
> return something;
>}
>
>Since Struts is able to reload a form if there are errors on it, this
>should be a simple matter. However, the code above just isn't working.
>It looks like even though we put an updated form object into the
>request or session, a new form object is being created when the
>request is forwarded.
>
>Any help, pointers to information, etc. would be greatly appreciated.
>
>Thank you.
>
>Dave Bruzzone
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Struts - Passing a form object "through" an action
Posted by Eddie Bush <ek...@swbell.net>.
I was going to tell you to add a 'name="formName"' to you action, but I
see you have it. Is it the same in both action declarations? It should
be. I can't think of any other reason that
you'd be having problems.
In your debugger - when action2 is rendered - is the form still there
with appropriate values? If it is, then my thought is that you've
misspelled the name of the form you want associated with action2. If
the state is different, my question is: Are you instantiating the
form-bean yourself and putting it "out there"?
There is an implicit contract between actions and action forms. You are
guaranteed that the action form will be instantiated for you before you
reach "execute". You don't need to worry about instantiating it
yourself. If for some reason it doesn't get instantiated (it really
should!), you'll get an NPE -- fail fast, and know there is an error
(which you should file a bug-report on!).
Regards,
Eddie
Davide Bruzzone wrote:
>Greetings all...
>
>I'm including a message that I posted on USENET a couple of days ago
>below... Any help/insight/someone pointing out what a silly mistake I'm
>making would be greatly appreciated:
>
>Wayne wrote:
>
>By default, a FormBean is session-scoped. That means it will be in the
>session context under its form name. Just go look at it and get the
>stuff you want.
>
>Wayne
>
><interjection>
>Yes, I can see the form bean in the session in my debugger... The values are
>correct (i.e. They are the values from the previous page). They just never
>make it back out to the page that's sent to the browser.
></interjection>
>
>I wrote:
>
>>Greetings all...
>>
>>Here's what we're trying to do: When an HTML form is posted to a
>>Struts action, we'd like to take the form object that's passed as a
>>parameter into the action method and somehow pass it back out to the
>>view (in our case a JSP) that the action forwards the request to (So
>>that the resulting view's values can be based on this form object's
>>attributes).
>>
>>i.e...
>>1) Form 1 has a field that contains the value "Foo"
>>2) The form is submitted to an action that forwards the request to
>>Form 2 (or back to Form 1). This second form also has a text field,
>>and it should contain the value "Foo" when it (be it Form 2 or Form 1)
>>is rendered.
>>
>>Here are some code snippets...
>>
>>- The JSP page:
>>
>><html:form action="/Test.do?action=view">
>> <html:select property="sel">
>> <%-- TestVector is the name of a Vector object in the
>>
>session --%>
>
>> <html:options name="TestVector"/>
>> </html:select>
>>
>> <input type="submit" value="Submit"/>
>></html:form>
>>
>>- struts-config.xml:
>>
>><form-beans type="org.apache.struts.action.ActionFormBean">
>> <form-bean name="TestForm" type="com.company.TestForm" />
>></form-beans>
>>
>>...
>>
>><action name="TestForm" path="/Test"
>> type="com.company.TestAction"
>> scope="request"
>> input="/test.jsp"
>> parameter="action">
>> <forward name="success" redirect="true" path="/test.jsp" />
>></action>
>>
>>- The appropriate action class code:
>>
>>...
>>
>>if (form == null) {
>> form = new TestForm();
>>
>> if ("request".equals(mapping.getScope()))
>> request.setAttribute(mapping.getAttribute(), form);
>> else
>> session.setAttribute(mapping.getAttribute(), form);
>>}
>>
>>TestForm testForm = (TestForm)form;
>>
>>testForm.setSomething("Two");
>>
>>- The appropriate form class code:
>>
>>private String something = null;
>>
>>public void setSomething(String newSomething) {
>> something = newSomething;
>>}
>>
>>public String getSomething() {
>> return something;
>>}
>>
>>Since Struts is able to reload a form if there are errors on it, this
>>should be a simple matter. However, the code above just isn't working.
>>It looks like even though we put an updated form object into the
>>request or session, a new form object is being created when the
>>request is forwarded.
>>
>>Any help, pointers to information, etc. would be greatly appreciated.
>>
>>Thank you.
>>
>>Dave Bruzzone
>>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>