Struts2的There is not Action mapped for namespace [/] 错误

Posted Richard_i

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2的There is not Action mapped for namespace [/] 错误相关的知识,希望对你有一定的参考价值。

错误原因让人啼笑皆非……重点内容**

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

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>test-struts</display-name>  
    <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>*.action</url-pattern>  
    </filter-mapping>  
    <welcome-file-list>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>  
</web-app>  

TestAction.java

package com.test.Action;  

import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Result;  
import org.apache.struts2.convention.annotation.Results;  

import com.opensymphony.xwork2.ActionSupport;  

@Results(value =  @Result(name = "success", location = "/result.jsp") )  
public class TestAction extends ActionSupport   

    private String name;  
    /** 
     *  
     */  
    private static final long serialVersionUID = 1L;  

    @Action("test")  
    public String test()   
        name = "qqqqqqq";  
        return SUCCESS;  
      

    public String getName()   
        return name;  
      

    public void setName(String name)   
        this.name = name;  
      

  

乍一看,是没有任何问题的!可是只要一运行就报错!
以下是Struts2的原文文档:
First the Convention plugin finds packages named struts, struts2, action 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处理类放在struts, struts2, action, actions包或者其子包中。
碰到了同样的问题,以前确实不知道基于注解的 struts 配置 需要将action类 放在 名字为 action 的package下。

以上是关于Struts2的There is not Action mapped for namespace [/] 错误的主要内容,如果未能解决你的问题,请参考以下文章

Struts2中There is no Action mapped for namespace错误解决方法

Struts2中There is no Action mapped for namespace错误解决方法

Struts2-tomcat报错:There is no Action mapped for namespace / and action

问题管理-- Struts2配置struts.xml中Action访问报There is no Action mapped for namespace...

Struts2异常:HTTP Status 404 - There is no Action mapped for action name addBook.

解决 struts2 报"There is no Action mapped for namespace / and action name"的问题