I have created a Dynamic Action class (DynaAction) that allows a page to
define where resulting action states should go.  You can define defaults
within the actual action or struts-config.xml and then override at the page
level.  The one drawback that I have seen is that since you are defining
your action states at the page level using hidden tags, it gives more
visibility into the application that what you otherwise might want to have.
To use the code below, extend from DynaAction and inside your class call
addState(String stateName, String URL) for each state that your action could
result in.  When you are ready to return the ActionMapping, call
loadState(String stateName, ActionMapping, HttpServletRequest).  If you need
to perform a redirect you can set redirect to true.

The code for DynaAction is below.

/*** The DynaAction

package com.belo.struts.action;

import com.belo.campaign.mail.BeloMail;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Hashtable;

/**
 *
 *      Extends Action to support dynamic forwarding, basic email mechanism,
 *      and hashtable creation.
 *
 *      @author Brian McClung
 *      @version $Revision: 1.8 $Date: 2004/01/21 23:23:38 $
 **/

public class DynaAction extends Action
{
        /**
         * the hashtable for class level mappings
         **/
        protected Hashtable stateHash;
        
        /**
         *      Any path that should be prepended to the URL in the
stateHash
         **/
         protected String forward = "";
        
        /**
         *      Tells the new Forward action to handle URL's as a page
redirect instead
         *      of a return;
         **/
        protected boolean redirect = false;
        
        /**
         *      Allows a fw= queryString parameter to either pass through to
the next page
         *      or be used to redirect the current page.
         **/
        protected boolean useFW = false;
        
                
        /**
         *      Default constructor
         **/
        public DynaAction()
        {
                stateHash = new Hashtable();
        }
        
        /**
         *
         *      Refactor of builcStateHash so action classes won't have to
implement them.
         *
         **/
        public void addState(String stateID, String URL)
        {
                stateHash.put(stateID, URL);
        }
        
        /**
         *      Sets the redirect flag for forwarding.
         **/    
        public void setRedirect(boolean redirect)
        {
                this.redirect = redirect;
        }
        
        /**
         *      Sets the path to be prepended to a URL before forwarding to
a page
         **/
        public void setForward(String forward)
        {
                if(forward == null)
                {
                        return;
                }
                this.forward = forward;
        }
        
        /**
         *      Returns the boolean value of useFW.
         *
         *      @return current boolean value of useFW
         **/
        public boolean getUseFW()
        {
                return useFW;
        }
        
        /**
         *      Sets the boolean value of useFW
         *      
         *      @param useFW - boolean value to set useFW to.
         **/
        public void setUseFW(boolean useFW)
        {
                this.useFW = useFW;
        }
        
        /**
         *
         *      Pulls the appropriate URL to forward to based on the state
received.
         *
         *      @param state  - the state this object is currently in.  Used
to look up the appropriate
         *                                      path for this object
         *      @param mappings  - the ActionMapping associated with this
object.
         *      @param request  - the HttpServletRequest associated with
this object
         *
         *      @return ActionForward  - a new ActionForward with the
appropriate forward information
         **/
        public ActionForward loadMapping(String state, ActionMapping
mapping, HttpServletRequest request)
        {
                ActionForward aForward = null;
                //Failure  -->  register.jsp
                //loginFailure  -->  signin.jsp
                //success  -->  thankyou.jsp

                //Pull forwards from struts-config.xml
                String fw = checkForward(request);              
                
                if(this.getUseFW())
                {
                        if( ( fw != null ) && ( !fw.equals("") ) )
                        {
                                return new ActionForward(state, fw, true);
                        }
                }
                
                String stateURL = (String) request.getParameter(state);
                
                if(stateURL != null)
                {
                        //page defined
                        aForward = new ActionForward(state, stateURL +
"?fw=" + fw, redirect);
                }
                else if (mapping.findForward(state) == null )
                {
                        //default
                        aForward = new ActionForward(state, forward +
(String) stateHash.get(state) + "?fw=" + fw, redirect);
                }
                else
                {
                        aForward = mapping.findForward(state);
                }
                
                return aForward;
        }
}





-----Original Message-----
From: Ramadoss Chinnakuzhandai [mailto:[EMAIL PROTECTED] 
Sent: Monday, February 23, 2004 3:38 PM
To: Struts Users Mailing List
Subject: RE: ActionClass

for example XYZAction class handle multiple request coming from several
different JSPs .....to identify the request I can pass some unique action
parameter and execute appropriate block.

My question is that is there any feature available is Struts so that I can
identify the request where it is coming from rather than define different
path against each request and associate that path with the XYZAction class.

Path would be the same(no action parameter passed)for all request but
XYZAction class must be smart enough to identify the source and redirect the
result to the page where it is coming from.

-Ramadoss







-----Original Message-----
From: Max Cooper [mailto:[EMAIL PROTECTED]
Sent: Monday, February 23, 2004 4:08 PM
To: Struts Users Mailing List
Subject: Re: ActionClass


Define what you mean by "where the request is coming from" and what kind of
processing you would like to optionally perform. It is not clear what you
are trying to accomplish -- give some more details so we can help.

-Max

----- Original Message -----
From: "Ramadoss Chinnakuzhandai" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, February 23, 2004 11:26 AM
Subject: ActionClass


Hi,
       Is there any features in Struts using which I can make the
ActionClass smart enough to understand where the request is coming from and
execute appropriate block ? If so could you pls drop some light on it?

Thank you in advance,

-Ramadoss






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to