Search code examples
javastrutsstruts-1struts-tags

cancel button without any action method throws and exception


I am having an issue with the Struts (1.x) code that I'm currently looking at (which is 12 years old) when a cancel button is clicked. Here's the java code with action methods:

public class CustomerApproveTypeAction extends AbstractCustomerApproveTypeAction {
    private final Log log = LogFactory.getLog(CustomerApproveTypeAction.class);
 
    public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'edit' method - QS: " + request.getQueryString());
        }
 
            return mapping.findForward("edit");
        }
 
         
        return mapping.findForward("home");
    }
 
    /**
     * Save the info from the screen (being edited)
     * 
     * 
     */
    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'save' method...");
        }
 
         
        return new ActionForward("/report.html?meth=viewResult&bc=" + kc);
    }
 
    /**
     * Approve the info from the screen (being edited)
     * 
     * 
     */
    public ActionForward approve(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'approve' method...");
        }
 
         
        return new ActionForward("/report.html?meth=viewResult&kc=" + kc);
    }
     
     
    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        return mapping.findForward("home");
    }
 
}

And here's how all the methods are called from the JSP (customerApproveType.jsp) and they all work fine when the Approve and Save buttons are clicked.

<tr>
            <td colspan="2" align="center">
<html:submit
                    styleClass="button" property="approve"
                    onclick="this.form.meth.value='approve'">Approve</html:submit>
                </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
<html:submit
                    styleClass="button" property="save"
                    onclick="this.form.meth.value='save'">Save</html:submit> <html:cancel
                    styleClass="button">Cancel</html:cancel></td>
        </tr>

However, as soon as cancel button is clicked, I keep getting:

[INFO] SEVERE: Servlet.service() for servlet [action] in context with path [/myapp] threw exception [java.lang.NoSuchMethodException: Action[/customerApproveType] does not contain specified method (check logs)] with root cause
[INFO] java.lang.NoSuchMethodException: Action[/customerApproveType] does not contain specified method (check logs)
 
so on and so forth ....
 
[INFO] org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

Here's my AbstractCustomerApproveTypeAction class:

public abstract class AbstractCustomerApproveTypeAction extends DispatchAction {
 
    public ActionMessages validateForm(ActionForm form, HttpServletRequest request) {
    }
 
    protected void mapFormToBean(UpdateCustomerForm updateCustomerForm, Order order) {
 
    }
 
    private void fillCustomer(CustomerHandler cph, Order order) {
 
    }
    protected CustomerHandler updateOrderUsingId(Order order, Integer userId) {
 
    }
    protected void updateCtrfile(Order order, Case cs) {
         
    }
    protected CustomerFile makeCustomerFile(Order order, Case cs, CustomerHandler cph, Manager mgr) {
         
        return customerFile;
    }
}

and struts-config.xml where the bean is defined for CustomerApproveTypeAction

<bean name="/customerApproveManual"
        class="com.abc.web.CustomerApproveTypeAction"
        scope="prototype">
        <property name="orderManager">
            <ref bean="orderManager" />
        </property>
         
</bean>

Since there is no onclick event of the cancel button like it's there for other buttons like onclick="this.form.meth.value='save'" (for example). Please explain if my thinking is incorrect here. I guess I can define an action method for the cancel button and call it like "onclick="this.form.meth.value='cancel'" but I was hoping that unspecified method would take care of it.

Edit:

Tried the approach mentioned in the answer and it still doesn't work.

public ActionForward cancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        return mapping.findForward("home");
    }

Solution

  • For <html:cancel> button there's already mapped a method named cancelled. All you need is just add this method to your action class.

    DispatchAction:

    If the value of the request parameter is empty, a method named unspecified is called. The default action is to throw an exception. If the request was cancelled (a <html:cancel> button was pressed), the custom handler cancelled will be used instead. You can also override the getMethodName method to override the action's default handler selection.