You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "McClung, Brian" <br...@belointeractive.com> on 2003/11/24 18:33:41 UTC

A Dynamic Action handler Opinons wanted.

I've been using struts for over two years now and enjoy the benefits that it
has brought to my code.  An issue that I have always had with it is the
struts-config.xml file.  We have a custom built registration and survey
system that struts is the controller for.  The backend engine supports most
of our surveys and contests as well as other backend processes.  For
contests we have a process where a new contest requires a creative designer
to create a signin, registration and thank you page for their contest and
then they include the standard signin and registration templates on their
pages.  The thank you page is simply a final page that everything can
resolve to.  When I originally started using struts the configuration file
had to be set up with 2 action-mappings pointing to each contest.  With 24
sites running contests the file rapidly became unmaintainable.  

The DynaAction was created to solve this problem.  Through a hashtable
created by each action object the DynaAction is able to redirect a single
action to many different pages.  I still use the state definitions as a
communication method, but I typically don't even define the states in the
struts-config.xml document any more.  The process is fairly simply, if there
is a field in the request that matches the state, then the value of that
field is used to redirect on that state.  If it is missing, the action looks
to the config file, and if that is missing then there is typically a default
page coded into the action itself.  

I'm curious to know what the group thinks about this extension and how it
fits in/breaks the struts architecture.  I'm surprised that something like
this doesn't already exist, I've found it to be a great addition to the API.
Any feedback or opinions about using a class like this in struts would be
appreciated.


package com.belo.struts.action;

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;

/**
 *  @class DynaAction
 *
 *	Extends Action to support dynamic forwarding, basic email mechanism,
 *	and hashtable creation.
 *
 *	@author Brian McClung
 *	@version $Revision: 1.5 $Date: 2003/11/19 23:24:26 $
 **/

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();
	}

	/**
	 *
	 *	Adds a state to the hashtable
	 *
	 *	@param stateID - the key to reference this state by
	 * 	@param URL - the value to associate with this state
	 **/
	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
	 *
	 *	@returns ActionForward  - a new ActionForward with the
appropriate forward information
	 **/
	public ActionForward loadMapping(String state, ActionMapping
mapping, HttpServletRequest request)
	{
		ActionForward aForward = null;

		//Pull forwards from struts-config.xml
		String fw = checkForward(request);
//		System.out.println("checked for the forward:" + fw);

		if(this.getUseFW())
		{
			if( ( fw != null ) && ( !fw.equals("") ) )
			{
				return new ActionForward(state, fw, true);
			}
		}

//		System.out.println("not using the forward");
		String stateURL = (String) request.getParameter(state);

		if(stateURL != null)
		{
			//page defined
//			System.out.println("Using StateURL:" + stateURL);
			aForward = new ActionForward(state, stateURL +
"?fw=" + fw, redirect);
		}
		else if (mapping.findForward(state) == null )
		{
			//default
//			System.out.println("Using the hashmap:" + state);
			aForward = new ActionForward(state, forward +
(String) stateHash.get(state) + "?fw=" + fw, redirect);
		}
		else
		{
//			System.out.println("Defaulting to the xml
document");
			aForward = mapping.findForward(state);
		}
//		System.out.println("aForward:" + aForward.getPath() );

		return aForward;
	}


	/**
	 *	Checks the request for the fw parameter.  If it exists, sets
the forward
	 *	variable.  Actions should use this parameter when it is
necessary to forward out of
	 *	the application.
	 *
	 *	@param request  - the HttpServletRequest object to pull the
forward from.
	 **/
	protected String checkForward(HttpServletRequest request)
	{
		//First try to pull the forward from the request
		String fw = (String) request.getParameter("fw");
		//If that pulls back nothing, try to get it from the
session.
		if(fw == null)
		{
			HttpSession sess = request.getSession();
			fw = (String) sess.getAttribute("fw");
		}

		//Regardless, do not return null.
		if (fw == null)
		{
			fw = "";
		}

		return fw;
	}
}

Thanks,

Brian McClung
Senior Programmer
Belo Interactive
214-977-4083


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org