Struts2 2.5版本执行index 不执行指定method

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yzk2356911358/article/details/78568028

报错  Wrong method was defined as an action method: index (Action类里面没有定义index这个方法)

错误原因 :

按以往的理解 为了实现项目的零配置,采用struts2的注解方式进行配置,会直接调getHelloWorld方法。
那么为何会报错呢?
看了Struts2的调用全过程,发现其实调用mapper的时候 是直接指向org.apache.struts2.dispatcher.mapper.Restful2ActionMapper 而不是DefaultActionMapper,
这样就导致了调用到rest里面的
if (mapping.getMethod() == null) {
    if (lastSlashPos == actionName.length() - 1) {
     if (isGet(request)) {
      mapping.setMethod("index");
     } else if (isPost(request)) {
      mapping.setMethod("create");
     }
    }
所以直接报NoSuchMethodException。
可配置文件中没有配置支持Restful2ActionMapper。
那是否struts2.3.24中jar包有插件直接支持呢?
果然发现了:struts2-rest-plugin-2.3.24.jar 这个包中的:struts-plugin.xml


    <bean type="com.opensymphony.xwork2.ActionProxyFactory" name="rest" class="org.apache.struts2.rest.RestActionProxyFactory" />
 <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="rest" class="org.apache.struts2.rest.RestActionMapper" />
    <constant name="struts.mapper.class" value="rest" />
这个导致了Restful2ActionMapper的调用。

解决方法:删除struts2-rest-plugin-xxx.jar


猜你喜欢

转载自blog.csdn.net/yzk2356911358/article/details/78568028