Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Re: Multiple buttons and validation

Niall Pemberton

2006-12-13

Replies:

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 <ericrank@(protected):
> 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@(protected)
> For additional commands, e-mail: user-help@(protected)
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.