使用版本:struts-2.5.12-all
出现问题:在开启动态方法调用后,找不到没有匹配的路径映射
<constant name="struts.enable.DynamicMethodInvocation" value="true" />默认是关闭的
2018-04-01 16:09:21,342 ERROR [http-nio-8080-exec-5] dispatcher.Dispatcher (Dispatcher.java:612) - Could not find action or result: /jinan_tourism_gis/user/user-login
com.opensymphony.xwork2.config.ConfigurationException: There is no Action mapped for namespace [/user] and action name [user-login] associated with context path [/jinan_tourism_gis].
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:198) ~[struts2-core-2.5.12.jar:2.5.12]
at org.apache.struts2.factory.StrutsActionProxy.prepare(StrutsActionProxy.java:62) ~[struts2-core-2.5.12.jar:2.5.12]
at org.apache.struts2.factory.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37) ~[struts2-core-2.5.12.jar:2.5.12]
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58) ~[struts2-core-2.5.12.jar:2.5.12]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:567) [struts2-core-2.5.12.jar:2.5.12]
at org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81) [struts2-core-2.5.12.jar:2.5.12]
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:143) [struts2-core-2.5.12.jar:2.5.12]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.2]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.2]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:9.0.2]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:9.0.2]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:9.0.2]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:9.0.2]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [catalina.jar:9.0.2]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) [catalina.jar:9.0.2]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:9.0.2]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [catalina.jar:9.0.2]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:501) [tomcat-coyote.jar:9.0.2]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.2]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754) [tomcat-coyote.jar:9.0.2]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376) [tomcat-coyote.jar:9.0.2]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.2]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_152]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_152]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.2]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_152]
解决办法:加入<global-allowed-methods>regex:.*</global-allowed-methods>,就可以正常使用动态方法调用,允许所有方法进行访问
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="user_*" class="com.zhoujian.gis.action.UserAction"
method="{1}">
<result name="success" type="redirect">/index.jsp</result>
<result name="logout">/logout.jsp</result>
</action>
</package>
也可以如下所示:加入<allowed-methods>login</allowed-methods>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user_*" class="com.zhoujian.gis.action.UserAction"
method="{1}">
<result name="success" type="redirect">/index.jsp</result>
<result name="logout">/logout.jsp</result>
<allowed-methods>login</allowed-methods>
<!-- 在这里可以是多个方法,使用","进行分隔 -->
</action>
</package>
原因:因为Struts在版本升级到2.5之后,在使用动态方法调用的时候,会先检查是否允许访问该方法,所以要标识哪些方法是可访问的。