解决There is no Action mapped for namespace [/] and action name [xxx] associated with context pat问题

1.今天struts单独使用时,用action_*通配符加占位符method={1}方式访问action对应的方法,运行正常,可是整合spring后,再用该方法就不好试了,报There is no Action mapped for namespace [/] and action name [xxx] associated with context pat,经过多方尝试,一步一步调试,终于找到了问题所在。

         

<struts.version>
2.5.20
</struts.version>

我用的版本是2.5的struts,它在web.xml中配置是

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

该版本如果这样配:

<action name="user_*" class="com.ssh.action.UserAction" method="{1}">
            <result>/success.jsp</result>
        </action>

就会将*匹配到的作为独立的类,也不会找对应的方法,只会报:

  • There is no Action mapped for namespace [/] and action name [test_login] associated with context path [].
  • 但是当我把版本换成
    <struts.version>2.3.24</struts.version>
  • 修改web.xml的配置 
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

再次运行,他就能找到对应方法,正常运行。

再查看了struts2.3和struts2.5的不同,发现:

struts2.3的  对于通配符+占位符组合模式的action方法的动态调用是默认开启的,但struts2.5为了更加安全和严谨,将其默认设置为关闭,所以需要在struts2.5版本的struts.xml中添加动态调用:

 <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <!--允许动态调用方法,通配符+占位符组合,必须加上这行代码-->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <constant name="struts.devMode" value="false"></constant>
    
    <package name="defaultPackage"  extends="struts-default">
       <!--报错时 Method 方法 for action Action is not allowed 添加global-allowed-methods
        表示允许动态调用的方法-->
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="test_*" class="com.ssh.action.TestActoin" method="{1}">
            <result name="success">/WEB-INF/test.jsp</result>
            <result name="login">/WEB-INF/login.jsp</result>
            <result name="register">/WEB-INF/register.jsp</result>
        </action>
    </package>
</struts>

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>mySSH</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
    <init-param>
      <param-name>struts.convention.classes.reload</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

最后总结下:

    1.  引用最新版本的jar包时一定要关注它与原来Ⅸ版本的不同之处(有哪些新的配置)

    2.dtd版本最好与新版本相同,避免不必要的错误。

   3.模块测试很有效。把出问题的模块单独剥离出来做测试,可以很快的发现并解决问题

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/88721547