Using the Struts MappingDispatchAction

No Gravatar

When you have an application with many actions and they tend to be fairly thin, you can use the MappingDispatchAction from struts to ease your headaches. Instead of having a class for every action, you can place multiple actions in one class file via the use of methods other than execute, the standard struts action method. Here are the various pieces.

In the action,


public class MyAction extends MappingDispatchAction{
    public ActionForward doSomething1(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
                //place code here
    }
    public ActionForward doSomething2(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
                //place code here
    }
}

In the struts-config.xml


	<action path="/actions/doSomething1.do"
			type="com.some.MyAction" parameter="doSomething1"
			name="MyForm" scope="request" validate="true">
		<forward name="success" path="/pages/myPage.jsp" />
        </action>
	<action path="/actions/doSomething2.do" type="com.some.MyAction"
			parameter="doSomething2" name="MyForm"
			scope="request" validate="true">
		<forward name="success" path="/pages/myPage2.jsp" />
        </action>

There you go, that’s all there is to it. Now you can have multiple methods in one action, and can handily reduce the amount of extraneous code in your application that’s just boilerplate action classes.

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *