Struts 1.x取消按钮触发NoSuchMethodException异常问题求助
核心原因
Struts 1.x的<html:cancel>标签有特殊处理逻辑:点击它提交表单时,会自动添加一个名为org.apache.struts.taglib.html.CANCEL的请求参数(值为true),DispatchAction会优先识别这个参数,跳过常规的方法分发流程(包括unspecified方法),直接查找配置中定义的cancel跳转规则。你之前添加cancelled方法无效,就是因为DispatchAction根本不会调用这个方法。
同时,异常中的HttpRequestMethodNotSupportedException是因为cancel请求的处理流程被打断,导致请求没有被正确路由到对应逻辑。
解决方案
方案1:在Struts配置中添加Cancel跳转规则
这是Struts处理cancel请求的标准方式,需要在struts-config.xml的对应action节点中添加<cancel>标签,指定跳转目标:
<action path="/customerApproveManual" type="org.springframework.web.struts.DelegatingActionProxy" name="updateCustomerForm" scope="request"> <!-- 已有跳转规则 --> <forward name="home" path="/home.jsp"/> <forward name="edit" path="/edit.jsp"/> <!-- 新增cancel跳转,直接指向home页面 --> <cancel name="home"/> </action>
注:你的Spring配置已将
CustomerApproveTypeAction注册为bean,Struts通过DelegatingActionProxy委托Spring管理Action实例,需确保struts-config.xml中的action配置正确关联该bean。
方案2:在Action中手动处理Cancel参数
如果不想修改配置,可在unspecified方法中主动判断cancel参数,手动处理跳转:
public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 检查是否为cancel请求 if (request.getParameter(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY) != null) { return mapping.findForward("home"); } // 其他未指定方法的请求处理逻辑 return mapping.findForward("home"); }
方案3:修改Cancel按钮的行为(备选)
若想让Cancel按钮和Save/Approve一样走meth参数分发逻辑,可将<html:cancel>改为普通<html:submit>并添加onclick事件:
<html:submit styleClass="button" onclick="this.form.meth.value='cancelled'">Cancel</html:submit>
此方式需保留你之前添加的cancelled方法,但违背了Struts cancel标签的设计初衷,仅作为临时备选。
验证注意事项
- 清理项目编译后的class文件,确保修改后的代码/配置生效
- 测试时可通过浏览器开发者工具查看Cancel按钮提交的请求参数,确认是否包含
org.apache.struts.taglib.html.CANCEL参数
内容的提问来源于stack exchange,提问作者Tan

