Action[/***] does not contain specified method (check logs)
今天利用SS1H框架整合做练习的时候,出现了一个Action中没有包含指定方法的Exception
异常信息:
2011-8-18 20:50:07 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.NoSuchMethodException: Action[/userAction] does not contain specified method (check logs)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:261)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
从头到尾检查了N遍配置文件以及页面调用传参等代码块都未发现问题。。
许久后终于……
错误代码:
原来问题出在方法的参数中。这也怪自己偷鸡不成蚀把米。
本想偷懒一下,直接利用重写DispatchAction类中的execute方法,改一下名字后直接使用。
没想到,粗心选错,重写了Action类中的execute,结果可想而知。。。
解决方案:
public ActionForward doQuery(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//内部实现略
}
顺便分享一下出现此异常的其他几种情况:
1、自定义Action类需要继承DispatchAction类。
2、方法参数ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response 顺序也需一致。
3、方法的访问修饰符设置为public、不是protected,更不是其他。
4、动态调用Action,在struts-config.xml中要配置Action的parameter参数,
参数是页面传值的参数,值与要执行Action中的方法名相同。
<action-mappings > <action path="/userAction" type="org.springframework.web.struts.DelegatingActionProxy" name="userForm" parameter="method" > <forward name="success" path="/index.jsp"></forward> <forward name="error" path="/error.jsp"></forward> </action> </action-mappings>
5、页面表单提交方式为post,否则也有可能出现这种异常。