Tom O.
Tom O.

Reputation: 5941

Sub-classing struts Action class

If Action class is sub-classed and the sub-class does not contain an 'execute' method, will struts automatically look to the super-class for an 'execute' method? If the parent class of the action sub-class is a sub-class of Action itself and does not contain an 'execute' method, will struts continue to look up the hierarchy until it finds a super-class that contains the 'execute' method? Does struts always look for and run the 'execute' method first? Below is some code which will hopefully clarify my question.

Super-class:

public class BaseXHRAction extends Action {
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String page = mapping.getParameter();
        String actionName = mapping.getPath();
        return mapping.findForward(page);
    }
}

Sub-class:

public class OdonohueTestInitXHRAction extends BaseXHRAction {
    //this class does not contain an 'execute' method
    //it contains some other method(s) below

    public ScreenConfigurationRes getResponse(ActionForm form, HttpServletRequest request, Context ctx) throws Exception {
        //return ScreenConfigurationRes
    }
}

struts-config.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" 
    "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <form-beans>
        <form-bean name="screenConfigurationForm" type="com.saic.ct.sys.presentation.forms.screenconfiguration.ScreenConfigurationForm"/>
    <form-beans>    

    <!-- Global Forwards -->
    <global-exceptions>
        <exception type="java.lang.Exception" key="Test" path="/errorAction.do"/>
    </global-exceptions>
    <global-forwards>
        <forward name="home" path="/home.do"/>
        <forward name="error" path="/errorAction.do"/>
        <forward name="logoutError" path="/loginInit.do"/>
    </global-forwards>

    <!-- Action Mappings -->
    <action-mappings>
        <action
            path="/OdonohueTestInitXHR"
            name="screenConfigurationForm"
            type="com.saic.ct.sys.presentation.actions.schedule.OdonohueTestInitXHRAction" 
            parameter="home"/>
    </action-mappings>

    <controller locale="true"/>

    <plug-in className="com.oroad.stxx.plugin.StxxPlugin">
        <set-property property="pipeline-config" value="/WEB-INF/stxx-pipelines.xml"/>
    </plug-in>

    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>
</struts-config>

Upvotes: 1

Views: 1220

Answers (1)

Roman C
Roman C

Reputation: 1

The execute() method is overridden.

When Struts performs an action it already has an instance of the Action. It is using that instance to call execute() method. It is a public method.

In Java all public methods are inherited. If your action doesn't override the execute() method then it uses the inherited method. Otherwise if you have overridden method and want to call the method of its superclass you can use super.

Even if Struts uses Action type to call execute() method a virtual method of the action class will be used because of polymorphism.

Of course it will search for virtual method in the object type's hierarchy in order from subclass to superclass. It's a Java nature, and Struts is just calling a method of the Action object.

Upvotes: 1

Related Questions