You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Hernandez, David" <da...@lehman.com> on 2007/11/15 17:46:50 UTC

Form Submission Question

Can anyone point me to a location that outlines the Form
submission/processing flow for struts 2? I'm having a bit of a problem
deciphering how forms are supposed to be handled. For instance I have a
Bean (Building) that I want to update (to a DB) from a form:

JSP:
<s:form action="setBuilding" method="POST">
	<s:textfield name="name" value="name" />
	<s:textfield name="streetAddress" value="streetAddress" />
	<s:textfield name="latitude" value="latitude" />
	<s:textfield name="longitude" value="longitude" />
	<s:checkbox  name="active" value="active" />
	<s:textfield name="zip" value="zip" />
</s:form>

Should I have the bean extend ActionSupport and implement the execute
method? That seems like it would be a violation of the MVC model . . .
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Form Submission Question

Posted by Ted Husted <hu...@apache.org>.
Patches are always welcome (once you get it figured out) :)

On Nov 15, 2007 2:22 PM, Hernandez, David <da...@lehman.com> wrote:
>
> Thanks guys. The bootstrap should have an example where the fields are
> populated by the Action Class's member variables. But I figured it out,
> I think . . .

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Form Submission Question

Posted by "Hernandez, David" <da...@lehman.com>.
Thanks guys. The bootstrap should have an example where the fields are
populated by the Action Class's member variables. But I figured it out,
I think . . . 

-----Original Message-----
From: Dave Newton [mailto:newton.dave@yahoo.com] 
Sent: Thursday, November 15, 2007 2:20 PM
To: Struts Users Mailing List
Subject: Re: Form Submission Question

--- Gary Affonso <gl...@greywether.com> wrote:
> Dave Newton wrote:
>> The action that *processes* the form submission may extend 
>> ActionSupport.
> 
> Dave knows that, of course, but I didn't want the emphasis on  
> "*processes*" above to make you think ActionSupport was only 
> appropriate for form processing actions.

Yep, that was a good clarificaton to make :)

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Form Submission Question

Posted by Ted Husted <hu...@apache.org>.
Just to further Dave is saying.

If the class is a proper JavaBean with a zero-argument constructor, if
it is null during autopopulation, then the framework will
automatically instantiate it.

For fancier implementations, we can even provide a custom type
converter for a class, that could do something like lookup a complete
City record from a database based on the ID.

 * http://struts.apache.org/2.x/docs/type-conversion.html

HTH, Ted.

On Nov 15, 2007 5:15 PM, Dave Newton <ne...@yahoo.com> wrote:
> Or you can put the city.id in the form as a hidden
> field.
>
> d.
>
> --- "Hernandez, David" <da...@lehman.com>
> wrote:
>
> >
>
> >  Sorry, The problem was that building has a member
> > variable that is a
> > class (City) which is going out of scope.
> > Building.toString() calls
> > city.getId() throwing the exception and returning
> > null . . .
> > Now, to keep that in scope somehow . . .
> >
> > -----Original Message-----
> > From: Hernandez, David
> > Sent: Thursday, November 15, 2007 3:12 PM
> > To: 'Struts Users Mailing List'
> > Subject: RE: Form Submission Question
> >
> > I'm having a problem with my bean going out of
> > scope. The form is
> > populated with the bean information fine, but when I
> > submit building is
> > null? How do I keep it in scope? Do I have to pull
> > it back out of the
> > ActionContext?
> >
> > public class BuildingFormTestAction extends
> > ActionSupport {
> >       private static final long serialVersionUID =
> > 6692492667406189130L;
> >       private Building building;
> >       public String init() throws Exception
> >       {
> >               //Fetch Building from db . . .
> >               return ActionSupport.SUCCESS;
> >       }
> >       public String execute() throws Exception
> >       {
> >               System.out.println("Submitting: ");
> >               System.out.println(building);//PRINTS NULL!
> >               return ActionSupport.SUCCESS;
> >       }
> >       public void setBuilding(Building b)
> >       {
> >               building = b;
> >       }
> >       public Building getBuilding()
> >       {
> >               return building;
> >       }
> > }
> >
> > Here's the JSP Form:
> > <s:form action="setBuilding" method="POST">
> >       <s:push value="building">
> >               <s:textfield label="Name" name="name" />
> >               <s:textfield label="Address" name="streetAddress"
> > />
> >               <s:textfield label="Lat" name="latitude" />
> >               <s:textfield label="Long" name="longitude" />
> >               <s:checkbox  label="Active" name="active" />
> >               <s:textfield label="Zip" name="zip" />
> >               <s:submit />
> >       </s:push>
> > </s:form>
> >
> > And Struts.xml exerpt:
> > <action name="form" class="BuildingFormTestAction"
> > method="init">
> >       <result>/buildingFormTest.jsp</result>
> >       <result name="input">/error.jsp</result>
> > </action> <action
> > name="setBuilding" class="BuildingFormTestAction"
> > method="execute">
> >       <result>/buildingFormTest.jsp</result>
> >       <result name="error">/error.jsp</result> </action>
> >
> > -----Original Message-----
> > From: Dave Newton [mailto:newton.dave@yahoo.com]
> > Sent: Thursday, November 15, 2007 2:20 PM
> > To: Struts Users Mailing List
> > Subject: Re: Form Submission Question
> >
> > --- Gary Affonso <gl...@greywether.com> wrote:
> > > Dave Newton wrote:
> > >> The action that *processes* the form submission
> > may extend
> > >> ActionSupport.
> > >
> > > Dave knows that, of course, but I didn't want the
> > emphasis on
> > > "*processes*" above to make you think
> > ActionSupport was only
> > > appropriate for form processing actions.
> >
> > Yep, that was a good clarificaton to make :)
> >
> > d.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Form Submission Question

Posted by Dave Newton <ne...@yahoo.com>.
Or you can put the city.id in the form as a hidden
field.

d.

--- "Hernandez, David" <da...@lehman.com>
wrote:

> 
>  Sorry, The problem was that building has a member
> variable that is a
> class (City) which is going out of scope.
> Building.toString() calls
> city.getId() throwing the exception and returning
> null . . .
> Now, to keep that in scope somehow . . .
> 
> -----Original Message-----
> From: Hernandez, David 
> Sent: Thursday, November 15, 2007 3:12 PM
> To: 'Struts Users Mailing List'
> Subject: RE: Form Submission Question
> 
> I'm having a problem with my bean going out of
> scope. The form is
> populated with the bean information fine, but when I
> submit building is
> null? How do I keep it in scope? Do I have to pull
> it back out of the
> ActionContext?
> 
> public class BuildingFormTestAction extends
> ActionSupport {
> 	private static final long serialVersionUID =
> 6692492667406189130L;
> 	private Building building;
> 	public String init() throws Exception 
> 	{
> 		//Fetch Building from db . . .
>     		return ActionSupport.SUCCESS;
>     	}
> 	public String execute() throws Exception
> 	{
> 		System.out.println("Submitting: ");
> 		System.out.println(building);//PRINTS NULL!
> 		return ActionSupport.SUCCESS;
> 	}
> 	public void setBuilding(Building b)
> 	{
> 		building = b;
> 	}
> 	public Building getBuilding()
> 	{
> 		return building;
> 	}
> }
> 
> Here's the JSP Form:
> <s:form action="setBuilding" method="POST">
> 	<s:push value="building">
> 		<s:textfield label="Name" name="name" />
> 		<s:textfield label="Address" name="streetAddress"
> />
> 		<s:textfield label="Lat" name="latitude" />
> 		<s:textfield label="Long" name="longitude" />
> 		<s:checkbox  label="Active" name="active" />
> 		<s:textfield label="Zip" name="zip" />
> 		<s:submit />
> 	</s:push>
> </s:form>
> 
> And Struts.xml exerpt:
> <action name="form" class="BuildingFormTestAction"
> method="init">
>   	<result>/buildingFormTest.jsp</result>
>     	<result name="input">/error.jsp</result>
> </action> <action
> name="setBuilding" class="BuildingFormTestAction"
> method="execute">
>     	<result>/buildingFormTest.jsp</result>
> 	<result name="error">/error.jsp</result> </action>
> 
> -----Original Message-----
> From: Dave Newton [mailto:newton.dave@yahoo.com]
> Sent: Thursday, November 15, 2007 2:20 PM
> To: Struts Users Mailing List
> Subject: Re: Form Submission Question
> 
> --- Gary Affonso <gl...@greywether.com> wrote:
> > Dave Newton wrote:
> >> The action that *processes* the form submission
> may extend 
> >> ActionSupport.
> > 
> > Dave knows that, of course, but I didn't want the
> emphasis on 
> > "*processes*" above to make you think
> ActionSupport was only 
> > appropriate for form processing actions.
> 
> Yep, that was a good clarificaton to make :)
> 
> d.
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - - - - - - - - - -
> 
> This message is intended only for the personal and
> confidential use of the designated recipient(s)
> named above.  If you are not the intended recipient
> of this message you are hereby notified that any
> review, dissemination, distribution or copying of
> this message is strictly prohibited.  This
> communication is for information purposes only and
> should not be regarded as an offer to sell or as a
> solicitation of an offer to buy any financial
> product, an official confirmation of any
> transaction, or as an official statement of Lehman
> Brothers.  Email transmission cannot be guaranteed
> to be secure or error-free.  Therefore, we do not
> represent that this information is complete or
> accurate and it should not be relied upon as such. 
> All information is subject to change without notice.
> 
> --------
> IRS Circular 230 Disclosure:
> Please be advised that any discussion of U.S. tax
> matters contained within this communication
> (including any attachments) is not intended or
> written to be used and cannot be used for the
> purpose of (i) avoiding U.S. tax related penalties
> or (ii) promoting, marketing or recommending to
> another party any transaction or matter addressed
> herein.
> 
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Form Submission Question

Posted by "Hernandez, David" <da...@lehman.com>.
 Sorry, The problem was that building has a member variable that is a
class (City) which is going out of scope. Building.toString() calls
city.getId() throwing the exception and returning null . . .
Now, to keep that in scope somehow . . .

-----Original Message-----
From: Hernandez, David 
Sent: Thursday, November 15, 2007 3:12 PM
To: 'Struts Users Mailing List'
Subject: RE: Form Submission Question

I'm having a problem with my bean going out of scope. The form is
populated with the bean information fine, but when I submit building is
null? How do I keep it in scope? Do I have to pull it back out of the
ActionContext?

public class BuildingFormTestAction extends ActionSupport {
	private static final long serialVersionUID =
6692492667406189130L;
	private Building building;
	public String init() throws Exception 
	{
		//Fetch Building from db . . .
    		return ActionSupport.SUCCESS;
    	}
	public String execute() throws Exception
	{
		System.out.println("Submitting: ");
		System.out.println(building);//PRINTS NULL!
		return ActionSupport.SUCCESS;
	}
	public void setBuilding(Building b)
	{
		building = b;
	}
	public Building getBuilding()
	{
		return building;
	}
}

Here's the JSP Form:
<s:form action="setBuilding" method="POST">
	<s:push value="building">
		<s:textfield label="Name" name="name" />
		<s:textfield label="Address" name="streetAddress" />
		<s:textfield label="Lat" name="latitude" />
		<s:textfield label="Long" name="longitude" />
		<s:checkbox  label="Active" name="active" />
		<s:textfield label="Zip" name="zip" />
		<s:submit />
	</s:push>
</s:form>

And Struts.xml exerpt:
<action name="form" class="BuildingFormTestAction" method="init">
  	<result>/buildingFormTest.jsp</result>
    	<result name="input">/error.jsp</result> </action> <action
name="setBuilding" class="BuildingFormTestAction" method="execute">
    	<result>/buildingFormTest.jsp</result>
	<result name="error">/error.jsp</result> </action>

-----Original Message-----
From: Dave Newton [mailto:newton.dave@yahoo.com]
Sent: Thursday, November 15, 2007 2:20 PM
To: Struts Users Mailing List
Subject: Re: Form Submission Question

--- Gary Affonso <gl...@greywether.com> wrote:
> Dave Newton wrote:
>> The action that *processes* the form submission may extend 
>> ActionSupport.
> 
> Dave knows that, of course, but I didn't want the emphasis on 
> "*processes*" above to make you think ActionSupport was only 
> appropriate for form processing actions.

Yep, that was a good clarificaton to make :)

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Form Submission Question

Posted by "Hernandez, David" <da...@lehman.com>.
I'm having a problem with my bean going out of scope. The form is
populated with the bean information fine, but when I submit building is
null? How do I keep it in scope? Do I have to pull it back out of the
ActionContext?

public class BuildingFormTestAction extends ActionSupport 
{
	private static final long serialVersionUID =
6692492667406189130L;
	private Building building;
	public String init() throws Exception 
	{
		//Fetch Building from db . . .
    		return ActionSupport.SUCCESS;
    	}
	public String execute() throws Exception
	{
		System.out.println("Submitting: ");
		System.out.println(building);//PRINTS NULL!
		return ActionSupport.SUCCESS;
	}
	public void setBuilding(Building b)
	{
		building = b;
	}
	public Building getBuilding()
	{
		return building;
	}
}

Here's the JSP Form:
<s:form action="setBuilding" method="POST">
	<s:push value="building">
		<s:textfield label="Name" name="name" />
		<s:textfield label="Address" name="streetAddress" />
		<s:textfield label="Lat" name="latitude" />
		<s:textfield label="Long" name="longitude" />
		<s:checkbox  label="Active" name="active" />
		<s:textfield label="Zip" name="zip" />
		<s:submit />
	</s:push>
</s:form>

And Struts.xml exerpt:
<action name="form" class="BuildingFormTestAction" method="init">
  	<result>/buildingFormTest.jsp</result>
    	<result name="input">/error.jsp</result>
</action>
<action name="setBuilding" class="BuildingFormTestAction"
method="execute">
    	<result>/buildingFormTest.jsp</result>
	<result name="error">/error.jsp</result>
</action>

-----Original Message-----
From: Dave Newton [mailto:newton.dave@yahoo.com] 
Sent: Thursday, November 15, 2007 2:20 PM
To: Struts Users Mailing List
Subject: Re: Form Submission Question

--- Gary Affonso <gl...@greywether.com> wrote:
> Dave Newton wrote:
>> The action that *processes* the form submission may extend 
>> ActionSupport.
> 
> Dave knows that, of course, but I didn't want the emphasis on  
> "*processes*" above to make you think ActionSupport was only 
> appropriate for form processing actions.

Yep, that was a good clarificaton to make :)

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Form Submission Question

Posted by Dave Newton <ne...@yahoo.com>.
--- Gary Affonso <gl...@greywether.com> wrote:
> Dave Newton wrote:
>> The action that *processes* the form submission
>> may extend ActionSupport.
> 
> Dave knows that, of course, but I didn't want the
> emphasis on  "*processes*" above to make you 
> think ActionSupport was only appropriate 
> for form processing actions.

Yep, that was a good clarificaton to make :)

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Form Submission Question

Posted by Gary Affonso <gl...@greywether.com>.
Dave Newton wrote:
> The action that *processes* the form submission may
> extend ActionSupport.

Yup.  Note that it's commonly not just "form processing actions" that 
extend ActionSupport, it's often all actions.

Dave knows that, of course, but I didn't want the emphasis on 
"*processes*" above to make you think ActionSupport was only appropriate 
for form processing actions.

Your beans do *not* extend ActionSupport.  They're typically referenced 
within the action as a standard property and getters/setters for that 
property.  That way your form/views have access to the bean's properties 
through a variety of view/form techniques.

- Gary

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Form Submission Question

Posted by Dave Newton <ne...@yahoo.com>.
The action that *processes* the form submission may
extend ActionSupport. You submit the form to an
action. The action either contains the fields of the
form or contains a bean that can be used on the form.

The bootstrap tutorial goes through a simple form
processing example.

http://struts.apache.org/2.x/docs/bootstrap.html

d.

--- "Hernandez, David" <da...@lehman.com>
wrote:

> 
> Can anyone point me to a location that outlines the
> Form
> submission/processing flow for struts 2? I'm having
> a bit of a problem
> deciphering how forms are supposed to be handled.
> For instance I have a
> Bean (Building) that I want to update (to a DB) from
> a form:
> 
> JSP:
> <s:form action="setBuilding" method="POST">
> 	<s:textfield name="name" value="name" />
> 	<s:textfield name="streetAddress"
> value="streetAddress" />
> 	<s:textfield name="latitude" value="latitude" />
> 	<s:textfield name="longitude" value="longitude" />
> 	<s:checkbox  name="active" value="active" />
> 	<s:textfield name="zip" value="zip" />
> </s:form>
> 
> Should I have the bean extend ActionSupport and
> implement the execute
> method? That seems like it would be a violation of
> the MVC model . . .
> - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - - - - - - - - - -
> 
> This message is intended only for the personal and
> confidential use of the designated recipient(s)
> named above.  If you are not the intended recipient
> of this message you are hereby notified that any
> review, dissemination, distribution or copying of
> this message is strictly prohibited.  This
> communication is for information purposes only and
> should not be regarded as an offer to sell or as a
> solicitation of an offer to buy any financial
> product, an official confirmation of any
> transaction, or as an official statement of Lehman
> Brothers.  Email transmission cannot be guaranteed
> to be secure or error-free.  Therefore, we do not
> represent that this information is complete or
> accurate and it should not be relied upon as such. 
> All information is subject to change without notice.
> 
> --------
> IRS Circular 230 Disclosure:
> Please be advised that any discussion of U.S. tax
> matters contained within this communication
> (including any attachments) is not intended or
> written to be used and cannot be used for the
> purpose of (i) avoiding U.S. tax related penalties
> or (ii) promoting, marketing or recommending to
> another party any transaction or matter addressed
> herein.
> 
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org