Struts2注解错误之There is no Action mapped for namespace [/]……

我用最新版本的Struts2.3.4做注解操作的时候,一直出现这个问题。花了近两个小时才解决,错误原因让人啼笑皆非……

[plain]  view plain copy
  1. There is no Action mapped for namespace [/] and action name [test] associated with context path [/test]. - [unknown location]  
  2.     at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)  
  3.     at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)  
  4.     at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)  
  5.     at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)  
  6.     at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:501)  
  7.     at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)  
  8.     at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)  
  9.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
  10.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
  11.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)  
  12.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)  
  13.     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)  
  14.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)  
  15.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)  
  16.     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)  
  17.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  
  18.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)  
  19.     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)  
  20.     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)  
  21.     at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1805)  
  22.     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)  
  23.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)  
  24.     at java.lang.Thread.run(Thread.java:722)  

web.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  4.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  5.     <display-name>test-struts</display-name>  
  6.     <filter>  
  7.         <filter-name>struts2</filter-name>  
  8.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  9.     </filter>  
  10.     <filter-mapping>  
  11.         <filter-name>struts2</filter-name>  
  12.         <url-pattern>*.action</url-pattern>  
  13.     </filter-mapping>  
  14.     <welcome-file-list>  
  15.         <welcome-file>index.jsp</welcome-file>  
  16.     </welcome-file-list>  
  17. </web-app>  


TestAction.java

[java]  view plain copy
  1. package com.test.Action;  
  2.   
  3. import org.apache.struts2.convention.annotation.Action;  
  4. import org.apache.struts2.convention.annotation.Result;  
  5. import org.apache.struts2.convention.annotation.Results;  
  6.   
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.   
  9. @Results(value = { @Result(name = "success", location = "/result.jsp") })  
  10. public class TestAction extends ActionSupport {  
  11.   
  12.     private String name;  
  13.     /** 
  14.      *  
  15.      */  
  16.     private static final long serialVersionUID = 1L;  
  17.   
  18.     @Action("test")  
  19.     public String test() {  
  20.         name = "qqqqqqq";  
  21.         return SUCCESS;  
  22.     }  
  23.   
  24.     public String getName() {  
  25.         return name;  
  26.     }  
  27.   
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.   
  32. }  

目录结构:

乍一看,是没有任何问题的!可是只要一运行就报错!在百思不得其解后,突然发现我这个包名建的有点特别,问题就出在这里!将Action换成action就可以了,以下是Struts2的原文文档:

 First the Convention plugin finds packages named strutsstruts2action or actions. Any packages that match those names are considered the root packages for the Convention plugin. Next, the plugin looks at all of the classes in those packages as well as sub-packages and determines if the classes implementcom.opensymphony.xwork2.Action or if their name ends with Action (i.e. FooAction).


注意我标红的内容,也就是说,如果要用Struts2的注解,你还非得将action处理类放在strutsstruts2action, actions包或者其子包中。以前总是习惯性的将所有的action处理类放在com.xxx.action下,以为仅仅是习惯而已,没曾想Struts2还就是这么规定的。


碰到了同样的问题,以前确实不知道基于注解的 struts 配置 需要将action类 放在 名字为 action 的package下。

所用JAR包:

commons-beanutils-1.7.0.jar
commons-codec-1.3.jar
commons-collections-3.2.jar
commons-digester-1.5.jar
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
commons-lang-2.3.jar
commons-logging-1.1.1.jar
commons-validator-1.3.1.jar
ezmorph-1.0.4.jar
freemarker-2.3.8.jar
jsf-api.jar
jsf-impl.jar
jstl-1.2.jar
ognl-2.6.11.jar
struts2-convention-plugin-2.2.1.1.jar
struts2-core-2.2.1.1.jar
xml-apis-1.0.b2.jar
xwork-core-2.2.1.1.jar




你可能感兴趣的:(Struts2注解错误之There is no Action mapped for namespace [/]……)