You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Shane Mingins <sh...@assureinternational.com> on 2003/05/22 22:48:52 UTC

Do I Use request.getParameter() or ActionForm.getXxxxx()

Hello

I have been reading through some struts documentation and using struts for
an application I am building.

Something I am confused about is the use of
request.getParameter("paramName") versus using the appropriate getter method
on the ActionForm bean in the execute method of the Action class.

If the attributes/values on a Jsp page are defined as properties on the
ActionForm bean then within the execute method of the Action class the form
parameter can be cast to the ActionForm passed and the getter method called
for each attribute.

Using the MailReader struts example to illustrate:

      Within the execute method of EditSubscriptionAction the action value
is retrieved with the following code:

	public final class EditSubscriptionAction extends Action {

    		public ActionForward execute(ActionMapping mapping,
				 			ActionForm form,
				 			HttpServletRequest
request,
							HttpServletResponse
response)
		throws Exception {

      		String action = request.getParameter("action");
    		}
	}

      But as the SubscriptionForm is available to the execute method and has
the action field defined could it not 	use:

	public final class EditSubscriptionAction extends Action {

    		public ActionForward execute(ActionMapping mapping,
				 			ActionForm form,
				 			HttpServletRequest
request,
							HttpServletResponse
response)
		throws Exception {

		SubscriptionForm subform = (SubscriptionForm) form;
		String action = subform.getAction();

    		}
	}


So my questions are:
1. Am I missing something? And if so, what?
2. Is there a preferred choice in retrieving values within the Action
execute method if the value is available to both     
   the HttpServletRequest and ActionForm objects passed to the method?

Thanks
Shane

Shane Mingins
Analyst Programmer
Assure NZ Ltd
Ph 644 494 2522



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


Re: Do I Use request.getParameter() or ActionForm.getXxxxx()

Posted by Ted Husted <hu...@apache.org>.
To demonstrate the various things you can do with Struts, the example 
MailReader application doesn't always demonstrate the best practice.

Most developers would define "action" as a property on the ActionForm 
and then retrieve it using the getAction method. But, if for any reason, 
someone preferred to get the parameter directly, there you go.

The same is true for the response parameter. 999 out of a thousand 
Actions will never use the response parameter. But occasionally you may 
have one that, rather than forward to a JSP, want to write the response 
itself. So, should that happen, there you go. =:0)

HTH, Ted.


Shane Mingins wrote:
> Hello
> 
> I have been reading through some struts documentation and using struts for
> an application I am building.
> 
> Something I am confused about is the use of
> request.getParameter("paramName") versus using the appropriate getter method
> on the ActionForm bean in the execute method of the Action class.
> 
> If the attributes/values on a Jsp page are defined as properties on the
> ActionForm bean then within the execute method of the Action class the form
> parameter can be cast to the ActionForm passed and the getter method called
> for each attribute.
> 
> Using the MailReader struts example to illustrate:
> 
>       Within the execute method of EditSubscriptionAction the action value
> is retrieved with the following code:
> 
> 	public final class EditSubscriptionAction extends Action {
> 
>     		public ActionForward execute(ActionMapping mapping,
> 				 			ActionForm form,
> 				 			HttpServletRequest
> request,
> 							HttpServletResponse
> response)
> 		throws Exception {
> 
>       		String action = request.getParameter("action");
>     		}
> 	}
> 
>       But as the SubscriptionForm is available to the execute method and has
> the action field defined could it not 	use:
> 
> 	public final class EditSubscriptionAction extends Action {
> 
>     		public ActionForward execute(ActionMapping mapping,
> 				 			ActionForm form,
> 				 			HttpServletRequest
> request,
> 							HttpServletResponse
> response)
> 		throws Exception {
> 
> 		SubscriptionForm subform = (SubscriptionForm) form;
> 		String action = subform.getAction();
> 
>     		}
> 	}
> 
> 
> So my questions are:
> 1. Am I missing something? And if so, what?
> 2. Is there a preferred choice in retrieving values within the Action
> execute method if the value is available to both     
>    the HttpServletRequest and ActionForm objects passed to the method?
> 
> Thanks
> Shane
> 
> Shane Mingins
> Analyst Programmer
> Assure NZ Ltd
> Ph 644 494 2522


-- 
Ted Husted,
Struts in Action <http://husted.com/struts/book.html>



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