You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Craig R. McClanahan" <Cr...@eng.sun.com> on 2001/02/16 19:55:08 UTC

Re: Conditional choice of action mapping based on html FORM and SELECT tag

Ellen Murray wrote:

> Hi,
>
> This is a newbie question. I am creating a web
> application where each page contains a pull down
> menu of commands (SELECT). I want the users choice
> of menu item to invoke the next action object.
> Because the FORM tag allows only one action,

This restriction is based on the corresponding restriction of the HTML
<form> element that is ultimately generated.

> I
> want that to be mapped to multiple actions in the
> struts-config.xml file. An example of what I'd
> like to do is below.
>
> Does anyone have a solution to this? I really need
> your help because I am working without access to
> other java or web programers.
>

The way you've set up your form, it looks like the input will include
the command parameter for you (since the default submit method is GET).
It's at the other end where the problem occurs, because matching against
your <action> elements is based on the "/planner" part of the URI --
without the request parameters.

Three strategies to consider:

* Subclass the ActionServlet servlet, and modify the
processActionMapping()
  method that chooses which action to execute.

* Use a single Action, but use "if" statements inside the action
  to decide what to do next.

* Use different actions (say, "/showReleases.do" and "/showTestPlan.do")

  and use JavaScript event handlers to change the form's "action"
attribute
  dynamically based on the choice in the SELECT box.

>
> Thanks - Ellen
>

Craig


>
> //
> // What I want in my web page
> //
>
> <FORM ACTION="planner.do" >
>
>  <SELECT name="cmd">
>    <option value="releases">ShowReleases"</OPTION>
>    <option value="testplan">ShowTestPlan"</OPTION>
>  </SELECT>
>
>  <INPUT type="submit" value="Submit">
>
>
> //
> // What I want in the struts-config.xml file...
> //
>  <!-- Display Releases -->
> <action  path="/planner?cmd=releases"
>           type="planner.ShowReleasesAction"
>          scope="request" >
>  <forward name="success"  path="/ShowReleases.jsp"/>
> </action>
>
>  <!-- Display Releases -->
> <action  path="/planner?cmd=testplan"
>           type="planner.ShowTestPlanAction"
>          scope="request" >
>  <forward name="success"  path="/ShowTestPlan.jsp"/>
> </action>
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35
> a year!  http://personal.mail.yahoo.com/


Re: Conditional choice of action mapping based on html FORM and SELECT tag

Posted by ma...@tumbleweed.com.
At 10:55 AM 2/16/01 -0800, Craig R. McClanahan wrote:
>Three strategies to consider:
>
>* Subclass the ActionServlet servlet, and modify the
>processActionMapping()
>   method that chooses which action to execute.
>
>* Use a single Action, but use "if" statements inside the action
>   to decide what to do next.
>
>* Use different actions (say, "/showReleases.do" and "/showTestPlan.do")
>
>   and use JavaScript event handlers to change the form's "action"
>attribute
>   dynamically based on the choice in the SELECT box.

Depending on how many commands you have and/or how sophisticated you want 
to get, another approach would be to follow Struts' lead: Define a 
SubAction (or Command) class, as a base class for all your command 
handlers, and use a HashMap to map cmd values to SubAction instances. Then 
your Action class just does the lookup and hands off to the appropriate 
SubAction.

--
Martin Cooper
Tumbleweed Communications