You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Thom Burnett <th...@gmail.com> on 2006/12/12 18:00:17 UTC
Multiple buttons and validation
I have a few buttons on my jsp page and I want to have validation happen on
some buttons and not on other buttons.
Is that possible? How? Any examples around?
Re: Multiple buttons and validation
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Thom,
Thom Burnett wrote:
> I have a few buttons on my jsp page and I want to have validation happen on
> some buttons and not on other buttons.
>
> Is that possible? How? Any examples around?
This question came up a few weeks ago, and I'll tell you what I told
whoever asked in the past. There may be other solutions, but this one is
fairly straightforward.
First, you create an action that is intended to handle the form
submission itself. No need for a form bean or anything like that (and,
of course, there's no validation). Just write it like this:
public ActionMapping execute(...)
{
if(null != request.getParameter("first_button"))
return mapping.findForward("first-forward");
else if(null != request.getParameter("second_button"))
return mapping.findForward("second-forward");
else if(null != request.getParameter("third_button"))
return mapping.findForward("third-forward");
.
.
.
}
Then, your first-, second-, and third-forwards can be defined to forward
(not redirect) the request to the "real" actions that do whatever you
want them to do. /Those/ actions should have form bean associated with
them and validation="true" in the mappings.
Remember that your "real" actions should all have the same "input" set
so that any validation errors will go back to the same page.
Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFfuiL9CaO5/Lv0PARAir4AKDCPozKfHQnaYXOzB+82nlmPmJuGQCeKQpH
C8fLHrm2tqDo2N/EKF+sbBA=
=6HwP
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Multiple buttons and validation
Posted by Niall Pemberton <ni...@gmail.com>.
Hey Eric, this is a great detailed response and would make a good wiki
page - theres one for the similar EventActionDispatcher - but your
answer looked more readable
http://wiki.apache.org/struts/EventActionDispatcher
if you feel like adding it I'm sure others would appreciate it down the road.
Niall
On 12/12/06, Eric Rank <er...@lo-fi.net> wrote:
> One of my favorite subclasses is EventDispatchAction. Check it out:
> http://struts.apache.org/1.x/struts-extras/apidocs/org/apache/struts/
> actions/EventDispatchAction.html
>
> Basically, it works by allowing you to define various methods in your
> Action class, which are tied to your various submit buttons. By
> defining specific methods depending on your type of form submission,
> you can manually validate your form when you want to.
>
> For example, if I have a form showing a record from a database, I
> might have 3 different submit buttons. "Delete", "Update", and
> "Retrieve". the code in the jsp would be like:
> <html:form action="multipleButtons">
> <html:submit property="action_delete">Delete</html:submit>
> <html:submit property="action_update">Update</html:submit>
> <html:submit property="action_retrieve">Retrieve</html:submit>
> </html:form>
>
> The action mapping will contain a "parameter" attribute where you'll
> name all of the potential ways that you might submit this form. The
> comma delineated values will match the values of the "property"
> attributes in your submit buttons. These will then link up to methods
> with the same name in your subclass of EventDispatchAction. Your
> action mapping in the struts-config.xml might look like the following.
>
> <action
> path="/multipleButtons"
> input="/WEB-INF/jsp/multipleButtons.jsp"
> type="MyEventDispatchAction"
> name="MyForm"
> validate="false"
> scope="request"
> parameter="action_delete,action_retrieve,action_update"
> >
> </action>
>
> Finally, your subclass of EventDispatchAction would look like this
>
> public class MyEventDispatchAction extends EventDispatchAction {
>
> //Executes when the "action_delete" button is selected
> public ActionForward action_delete(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest request,
> HttpServletResponse response
> )throws Exception{
> //no validation
> ActionForward forward = mapping.findForward("success");
> return forward;
> }
>
> //Executes when the "action_update" button is selected
> //This is where I want to validate
> public ActionForward action_update(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest request,
> HttpServletResponse response
> )throws Exception{
> ActionForward forward = mapping.getInputForward();
> //I want validation here!
> ActionMessages errors = form.validate();
> if(errors.isEmpty){
> //go ahead and save the record
> forward = mapping.findForward("success");
> }
> return forward;
> }
>
> //Executes when the "action_retrieve" button is selected
> public ActionForward action_retrieve(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest request,
> HttpServletResponse response
> )throws Exception{
> //no validation
> ActionForward forward = mapping.findForward("success");
> return forward;
> }
> }
>
> I've had good success using this class. I hope it fits the bill for you.
>
> Eric Rank
>
>
> On Dec 12, 2006, at 10:00 AM, Thom Burnett wrote:
>
> > I have a few buttons on my jsp page and I want to have validation
> > happen on
> > some buttons and not on other buttons.
> >
> > Is that possible? How? Any examples around?
>
>
> ---------------------------------------------------------------------
> 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: Multiple buttons and validation
Posted by Eric Rank <er...@lo-fi.net>.
One of my favorite subclasses is EventDispatchAction. Check it out:
http://struts.apache.org/1.x/struts-extras/apidocs/org/apache/struts/
actions/EventDispatchAction.html
Basically, it works by allowing you to define various methods in your
Action class, which are tied to your various submit buttons. By
defining specific methods depending on your type of form submission,
you can manually validate your form when you want to.
For example, if I have a form showing a record from a database, I
might have 3 different submit buttons. "Delete", "Update", and
"Retrieve". the code in the jsp would be like:
<html:form action="multipleButtons">
<html:submit property="action_delete">Delete</html:submit>
<html:submit property="action_update">Update</html:submit>
<html:submit property="action_retrieve">Retrieve</html:submit>
</html:form>
The action mapping will contain a "parameter" attribute where you'll
name all of the potential ways that you might submit this form. The
comma delineated values will match the values of the "property"
attributes in your submit buttons. These will then link up to methods
with the same name in your subclass of EventDispatchAction. Your
action mapping in the struts-config.xml might look like the following.
<action
path="/multipleButtons"
input="/WEB-INF/jsp/multipleButtons.jsp"
type="MyEventDispatchAction"
name="MyForm"
validate="false"
scope="request"
parameter="action_delete,action_retrieve,action_update"
>
</action>
Finally, your subclass of EventDispatchAction would look like this
public class MyEventDispatchAction extends EventDispatchAction {
//Executes when the "action_delete" button is selected
public ActionForward action_delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
)throws Exception{
//no validation
ActionForward forward = mapping.findForward("success");
return forward;
}
//Executes when the "action_update" button is selected
//This is where I want to validate
public ActionForward action_update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
)throws Exception{
ActionForward forward = mapping.getInputForward();
//I want validation here!
ActionMessages errors = form.validate();
if(errors.isEmpty){
//go ahead and save the record
forward = mapping.findForward("success");
}
return forward;
}
//Executes when the "action_retrieve" button is selected
public ActionForward action_retrieve(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
)throws Exception{
//no validation
ActionForward forward = mapping.findForward("success");
return forward;
}
}
I've had good success using this class. I hope it fits the bill for you.
Eric Rank
On Dec 12, 2006, at 10:00 AM, Thom Burnett wrote:
> I have a few buttons on my jsp page and I want to have validation
> happen on
> some buttons and not on other buttons.
>
> Is that possible? How? Any examples around?
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org