You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Erik Hatcher <ja...@ehatchersolutions.com> on 2001/11/14 22:20:57 UTC

[SUBMIT] LookupDispatchAction - how to handle multiple html:submit buttons

As promised earlier today, here is my contribution to the multiple
html:submit button saga (Ted, time to update your FAQ! :)....

Here is a breakdown of how to use it:

struts-config.xml segment, note the parameter="action"
    <action path="/test"
            type="edu.darden.TestAction"
            name="TestForm"
            scope="request"
            input="/test.jsp"
            validate="true"
            parameter="action">
        <forward name="success" path="/results.jsp"/>
    </action>

ApplicationResources.properties segment:
    button.add=Add Record
    button.delete=Delete Record

TestAction.java (extends LookupDispatchAction) segment, the map is keyed by
ApplicationResources.properties
keys, and the value is the method name you want called (with the same
signature as Action.perform):

protected Map getKeyMethodMap(ActionMapping mapping,
                                                        ActionForm form,
                                                        HttpServletRequest
request) {
    Map map = new HashMap();
    map.put("button.add", "add");
    map.put("button.delete", "delete");
    return map;
}

public ActionForward add(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response)
                        throws IOException, ServletException {
    System.out.println(">>> ADD!!!!!");
    return null;
}

public ActionForward delete(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
                        throws IOException, ServletException {
    System.out.println(">>> DELETE!!!!!");
    return null;
}

And finally the JSP code segment:
    <html:form action="/test">
        <html:submit property="action">
            <bean:message key="button.add"/>
        </html:submit>
        <html:submit property="action">
            <bean:message key="button.delete"/>
        </html:submit>
    </html:form>

I recommend the above usage in most cases to cut down on the 'if' statements
and making each unique action clearly defined in separate methods.

But, if there is ever a need to be more general with it, I have provided in
my base class the flexibility to handle dispatching to a single method for
all keys, providing the key to the method. I can only see this being useful
if you have a whole bunch of buttons and there is little difference in the
actions they perform and you only need to do minor logic based on the key.

Here is an example of this, note that if you implement getKeys (and do not
return null) that getKeyMethodMap is not used, so in all but the most crazy
cases would you ever implement both getKeys and getKeyMethodMap. I provided
the mapping, form, and request to these methods just in case someone wanted
to do some dynamic crazy stuff with the array or Map returned from these
methods, but I don't see those parameters being used in most cases.

protected String[] getKeys(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request) {
    return new String[] {"button.add", "button.delete" };
}

protected ActionForward perform(String key,
                                        ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response)
                                throws IOException, ServletException {
    System.out.println(">>>> key = " + key);
    return null;
}

I am out of town for a long weekend tomorrow, so I wanted to get this done
today and get it out to struts-dev for feedback.  I plan on providing Cactus
or Deryl's mock test cases for this (but if anyone wants to beat me to
generating those, feel free!) - for this I really just want to verify that
the correct methods get dispatched with the proper key and that error
handling is dealt with appropriately so Cactus isn't necessary.  I based it
loosely off of DispatchAction.  It would be nice if my invokeMethod was
actually pushed up to DispatchAction to avoid duplicate code and
DispatchAction refactored to use this helper method.

I believe I might have made this class a little too feature-rich and perhaps
it should be broken into two base classes - one to dispatch via reflection
to a Map specified mapping between key and method, and another to do the
dispatching to an enhanced perform method taking the key.  With two separate
base classes the getKeys, getKeyMethodMap and perform could all be made
abstract and remove some likely usage confusion.

My Javadoc comments need some work to encompass both ways of using my base
class, but both methods are documented in this message for now.

I welcome feedback and suggestions on this.

    Erik



Re: [SUBMIT] LookupDispatchAction - how to handle multiple html:submit buttons

Posted by Ted Husted <hu...@apache.org>.
Most of us would not rely *solely* on Javascript for validation, since
it can be turned off, and therefor endanger your application. But
requiring it to handle multiple buttons is a different matter, so long
as the default formAction did nothing harmful if Javascript were turned
off. 

The ValidatorForm in the contrib folder generates *lots* of Javascript.
The html:form tag also generates a Javascript to move the focus, as do
some others. So, I don't see anything wrong with a tag that generated
Javascript for setting a property. Heck, I'm doing by hand myself ;-)

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel +1 716 737-3463
-- http://www.husted.com/struts/


Joe Faith wrote:
> 
> We have also implemented a multiple submit button system; but in our
> case we wrote a custom tag that generates javascript that sets a property
> to a specified value and submits the form when you hit the button.
> 
> The tags look something like:
> 
>     <my_submit property="formAction" value="delete" label="Delete this
> record"/>
>     <my_submit property="formAction" value="save" label="Save changes"/>
>     <my_submit property="formAction" value="copy" label="Copy this record"/>
> 
> etc
> 
> A single action object can then use the value of formAction to decide what to
> do in each case.
> This has the side-effect of reducing the number of Action classes needed.
> 
> However, the tags generate javascript and I wasn't sure if this fitted the
> struts philosophy; so I
> haven't submitted the code changes. But if anyone was interested I could make
> it available.
> 
> Joe Faith
> 
> Runtime Collective, Ltd
> T: (+44) 01273 234294

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [SUBMIT] LookupDispatchAction - how to handle multiple html:submit buttons

Posted by Joe Faith <fa...@runtime-collective.com>.
Here's the code and the .tld.

At the moment the SubmitTag class is in our own package
(com.RuntimeCollective.struts)
but it would be trivial to move it.

(I didn't integrate it into the struts:html tag library, cos I wasn't sure if
it was appropriate)

Joe Faith
http://www.runtime-collective.com
T: (+44) 01273 234294
M: (+44) 07968 292064

Andy Noble wrote:

> Hi Joe,
>
> I would certainly be interested in your code for this tag, as I have decided
> to make javascript a prerequisite for my project.
>
> Regards
> Andy
>
> ----- Original Message -----
> From: Joe Faith <fa...@runtime-collective.com>
> To: Struts Developers List <st...@jakarta.apache.org>
> Sent: Thursday, November 15, 2001 10:08 AM
> Subject: Re: [SUBMIT] LookupDispatchAction - how to handle multiple
> html:submit buttons
>
> > We have also implemented a multiple submit button system; but in our
> > case we wrote a custom tag that generates javascript that sets a property
> > to a specified value and submits the form when you hit the button.
> >
> > The tags look something like:
> >
> >     <my_submit property="formAction" value="delete" label="Delete this
> > record"/>
> >     <my_submit property="formAction" value="save" label="Save changes"/>
> >     <my_submit property="formAction" value="copy" label="Copy this
> record"/>
> >
> > etc
> >
> > A single action object can then use the value of formAction to decide what
> to
> > do in each case.
> > This has the side-effect of reducing the number of Action classes needed.
> >
> > However, the tags generate javascript and I wasn't sure if this fitted the
> > struts philosophy; so I
> > haven't submitted the code changes. But if anyone was interested I could
> make
> > it available.
> >
> > Joe Faith
> >
> > Runtime Collective, Ltd
> > T: (+44) 01273 234294
> >
> > Erik Hatcher wrote:
> >
> > > As promised earlier today, here is my contribution to the multiple
> > > html:submit button saga (Ted, time to update your FAQ! :)....
> >
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

Re: [SUBMIT] LookupDispatchAction - how to handle multiple html:submit buttons

Posted by Andy Noble <an...@data-workshop.com>.
Hi Joe,

I would certainly be interested in your code for this tag, as I have decided
to make javascript a prerequisite for my project.

Regards
Andy

----- Original Message -----
From: Joe Faith <fa...@runtime-collective.com>
To: Struts Developers List <st...@jakarta.apache.org>
Sent: Thursday, November 15, 2001 10:08 AM
Subject: Re: [SUBMIT] LookupDispatchAction - how to handle multiple
html:submit buttons


> We have also implemented a multiple submit button system; but in our
> case we wrote a custom tag that generates javascript that sets a property
> to a specified value and submits the form when you hit the button.
>
> The tags look something like:
>
>     <my_submit property="formAction" value="delete" label="Delete this
> record"/>
>     <my_submit property="formAction" value="save" label="Save changes"/>
>     <my_submit property="formAction" value="copy" label="Copy this
record"/>
>
> etc
>
> A single action object can then use the value of formAction to decide what
to
> do in each case.
> This has the side-effect of reducing the number of Action classes needed.
>
> However, the tags generate javascript and I wasn't sure if this fitted the
> struts philosophy; so I
> haven't submitted the code changes. But if anyone was interested I could
make
> it available.
>
> Joe Faith
>
> Runtime Collective, Ltd
> T: (+44) 01273 234294
>
> Erik Hatcher wrote:
>
> > As promised earlier today, here is my contribution to the multiple
> > html:submit button saga (Ted, time to update your FAQ! :)....
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [SUBMIT] LookupDispatchAction - how to handle multiple html:submit buttons

Posted by Joe Faith <fa...@runtime-collective.com>.
We have also implemented a multiple submit button system; but in our
case we wrote a custom tag that generates javascript that sets a property
to a specified value and submits the form when you hit the button.

The tags look something like:

    <my_submit property="formAction" value="delete" label="Delete this
record"/>
    <my_submit property="formAction" value="save" label="Save changes"/>
    <my_submit property="formAction" value="copy" label="Copy this record"/>

etc

A single action object can then use the value of formAction to decide what to
do in each case.
This has the side-effect of reducing the number of Action classes needed.

However, the tags generate javascript and I wasn't sure if this fitted the
struts philosophy; so I
haven't submitted the code changes. But if anyone was interested I could make
it available.

Joe Faith

Runtime Collective, Ltd
T: (+44) 01273 234294

Erik Hatcher wrote:

> As promised earlier today, here is my contribution to the multiple
> html:submit button saga (Ted, time to update your FAQ! :)....