users
[Top] [All Lists]

Re: [cinjug-users] Struts basic question

To: Sling Shot <slingshaut@xxxxxxxxx>
Subject: Re: [cinjug-users] Struts basic question
From: Greg Williams <greg@xxxxxxxxxxxxxxxx>
Date: Fri, 23 Jul 2004 22:12:22 -0400
Cc: users@xxxxxxxxxx
Delivered-to: mailing list users@cinjug.org
In-reply-to: <20040723221708.22308.qmail@web50003.mail.yahoo.com>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
References: <20040723221708.22308.qmail@web50003.mail.yahoo.com>
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040113
Sling,

Yes, your action class can set the action forward value to any String you want.  Most commonly used is success and failure, but they can be any number of user defined Strings (including success and failure).  Then in your action definition in the struts-config.xml,  you map each forward value to it's forward action.  See the example below.

Example from  struts-config.xml

<action name="MyForm" type="com.foo.action.MyAction" path="/displayChoice" />
    <forward name="default" path="/displayChoice.do" />
    <forward name="dothis" path="/thisAction.do" />
    <forward name="dothat" path="/thatAction.do" />
    <forward name="dotheother" path="/theotherAction.do" />
</action>

Example com.foo.action.MyAction.java:

package com.foo.action.MyAction

import org.apache.struts.action.*;
// other imports

public class MyAction extends Action
{
    public ActionForward execute (ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
    {
       String forward = "default";

        /* your useful business logic here */

       // decide where to branch to here
        if (someCondition1)
          forward = "dothis";
       else if (someCondition2)
          forward = "dothat";
       else if (someCondition3)
          forward = "dotheother";
      
       return mapping.findForward(forward);  
    }
}

In the example above, if none of the conditions are met, the default action occurs -- which is redisplaying the same page.

Hope this helps.

Greg

Sling Shot wrote:
Hi,
 
Is there a way in struts to provide a "branching action"?
What we have is a wizard style web interface which walks through
a seqeunce of forms. However in one instance, the next page in sequence
depends on one of the input choices selected by the user on the current
page.
 
I looked at ForwardAction and SwitchAction, but i dont think any of them
satisfies this problem. Since i think this is a fairly common scenario,
I am wondering if there is a way to handle this via configuration in struts.
 
Currently we are using an Action class to detect the user input and forward
to the appropriate action.
 
I would like to know if anyone knows of an alternative/recommended way of handling this scenario.
 
Thanks,
/sling


Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
<Prev in Thread] Current Thread [Next in Thread>