You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Stanley Santiago <st...@netscape.com> on 2000/07/26 07:45:33 UTC
Multiple actions per Form
Hi,
I just started exploring the Struts Framework and have a question.
The framework seems to assume that there will always be only action
associated
with a Form. The example in the documentation has a login example like
this:
<struts:form action="logon.do" name="logonForm"
type="org.apache.struts.example.LogonForm"/>
....
In this case the form is processed using the action mapping for
"login.do". But how
would I define multiple actions for the same form.
For example: a subscription list form can have three associated actions
subscribe.do, unsubscribe.do,
upgrade.do.
I understand I can use hidden elements or use the submit tag to pass
some extra info, but was looking for
a better more elegant solution.
Thanks,
Stan
Re: Multiple actions per Form
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Stanley Santiago wrote:
> Hi,
>
> I just started exploring the Struts Framework and have a question.
>
> The framework seems to assume that there will always be only action
> associated
> with a Form. The example in the documentation has a login example like
> this:
> <struts:form action="logon.do" name="logonForm"
> type="org.apache.struts.example.LogonForm"/>
> ....
> In this case the form is processed using the action mapping for
> "login.do". But how
> would I define multiple actions for the same form.
>
> For example: a subscription list form can have three associated actions
> subscribe.do, unsubscribe.do,
> upgrade.do.
>
> I understand I can use hidden elements or use the submit tag to pass
> some extra info, but was looking for
> a better more elegant solution.
>
The issue here is a limitation (if you want to call it that) in the
underlying HTML <form> tag.
A <struts:form> tag like this:
<struts:form action="logon.do" name="logonForm"
type="org.apache.struts.example.LogonForm"/>
gets converted into an HTML tag that looks like this:
<form method="POST" action="logon.do">
which only allows a single action. The action, in fact, specifies the URL
to which the form data will be submitted -- and HTML allows you to specify
only one per form (you can, of course, have multiple forms per page if you
want to).
For the type of scenario you are talking about, I normally use a hidden
variable called "action" to choose which function should be performed by the
(single) action URI to which the form is submitted -- in Struts terms, to
the single Action class whose perform() method is ultimately called.
However, you can generate the hidden variable's value dynamically based on a
bean property or request parameter passed from the Action class to the JSP
page.
For example, note how the "registration.jsp" page in the example app
generates different hidden values (and even a different page title) based on
the value of the "action" property of the "registrationForm" bean. The
original value is set by whichever Action class to "Create" or "Edit"
depending on how the user navigated to this screen. When submitted, the
value of the action property will tell the "saveRegistration.do" procedure
to create a new registration or update the old one.
>
> Thanks,
> Stan
Craig
Re: Multiple actions per Form
Posted by Stanley Santiago <st...@netscape.com>.
Giuseppe Galli wrote:
> Hi all,
> what about to use three different form in the same page?
> I always _try_ to use this rule: one action for each form, especially when
> (is this your case?) you need the form values only in one action and not in
> other 2.
That might be true in some cases. But that is a decision I would like to
defer to the page designers and not enfore in UI page generation framework.
The previous reply I got on this thread, used Javascript to solve this problem.
That works for me and is elegant, but then Javascript becomes a minimum
requirement
for the UI which again might not be always true ( luckily, it is for the
current project I am working in).
Another approach could be to extend the action mapping/controller mechanism to
handle
this automatically. This tries to mimic the javascript example.
So the form could like this :
<form action="checkaction.do" >
..
..
<input type="submit" name="submit.do" value="submit">
<input type="submit" name="cancel.do" value="cancel">
..
..
checkaction.do coulld be considered a special case, in which case the
controller will
check the request data for the "real" action.
Any thoughts on this approach ?
Thanks,
Stan
>
>
> Another way is to use a single action that checks if in the request is
> present the param relative to the input button: if its not null the user has
> clicked on that button
> eg:
> <form action="oneaction.do" >
> ..
> <input type="submit" name="submit" value="submit">
> <input type="submit" name="cancel" value="cancel">
> ..
>
> handling the request:
>
> if (null !=req.getParamValue( "cancel" ) ) there's no doubt about user
> choice.
>
> Yes, I know it's not so elegant, but it works..
>
> Hope this helps.
> Ciao
> Pino
>
> Stanley Santiago wrote:
>
> > Hi,
> >
> > I just started exploring the Struts Framework and have a question.
> >
> > The framework seems to assume that there will always be only action
> > associated
> > with a Form. The example in the documentation has a login example like
> > this:
> > <struts:form action="logon.do" name="logonForm"
> > type="org.apache.struts.example.LogonForm"/>
> > ....
> > In this case the form is processed using the action mapping for
> > "login.do". But how
> > would I define multiple actions for the same form.
> >
> > For example: a subscription list form can have three associated actions
> > subscribe.do, unsubscribe.do,
> > upgrade.do.
> >
> > I understand I can use hidden elements or use the submit tag to pass
> > some extra info, but was looking for
> > a better more elegant solution.
> >
> > Thanks,
> > Stan
RE: Multiple actions per Form
Posted by Kevin Duffey <kd...@buymedia.com>.
I do the following:
<form action="" method="post">
...
...
<input type="submit" onclick="this.action='/myaction1.do'"
value="unsubscribe">
<input type="submit" onclick="this.action='/myaction2.do'"
value="subscribe">
<input type="submit" onclick="this.action='/myaction3.do'" value="upgrade">
</form>
I can't remember if its this.action, or this.form.action, or
form.this.action..something like that. But using the onclick javascript
works well in this situation, and you simply "change" the forms action
attribute to the action to call.
> -----Original Message-----
> From: Stanley Santiago [mailto:stansa@netscape.com]
> Sent: Tuesday, July 25, 2000 10:46 PM
> To: struts-user@jakarta.apache.org
> Subject: Multiple actions per Form
>
>
> Hi,
>
> I just started exploring the Struts Framework and have a question.
>
> The framework seems to assume that there will always be only action
> associated
> with a Form. The example in the documentation has a login example like
> this:
> <struts:form action="logon.do" name="logonForm"
> type="org.apache.struts.example.LogonForm"/>
> ....
> In this case the form is processed using the action mapping for
> "login.do". But how
> would I define multiple actions for the same form.
>
> For example: a subscription list form can have three associated actions
> subscribe.do, unsubscribe.do,
> upgrade.do.
>
> I understand I can use hidden elements or use the submit tag to pass
> some extra info, but was looking for
> a better more elegant solution.
>
> Thanks,
> Stan
>
>