Struts Hello world示例:没有映射名称空间[/]和操作名称错误的Action

时间:2012-01-24 17:50:55

标签: struts2 struts2-namespace action-mapping

我绝对是Struts2的初学者。我想跟随struts网站上的教程。我跟着this tutorial。我有点麻烦。我在eclipse上创建了动态web项目。然后我按照教程。但是,当我运行该示例时,我收到以下错误。

There is no Action mapped for namespace [/] and action name [hello] associated with context path [/Hello_World_Struts_2]. - [unknown location]

我有以下目录结构

enter image description here

我的struts.xml文件是

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <constant name="struts.devMode" value="true" />

  <package name="basicstruts2" extends="struts-default" namespace="/">

  <action name="index">
    <result>/index.jsp</result>
  </action>

  <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
    <result name="SUCCESS">/HelloWorld.jsp</result>
  </action>

</package>

</struts>

感谢您的回复。

7 个答案:

答案 0 :(得分:17)

struts.xml配置文件需要在类路径上(而不是WEB-INF)。

链接教程假定Maven构建并声明struts.xml文件应该放在src/main/resources中,它将包含在Maven构建的类路径中。由于您忽略了该部分,因此您可能希望将其放在源目录的根目录中。

答案 1 :(得分:3)

您必须在classes/下创建一个WEB-INF/文件夹,并将其放入struts.xml文件中!

答案 2 :(得分:2)

右键单击该项目。转到属性部署构建路径并将所有.jar文件添加到/ WEB-INF / lib。
然后您不应该收到此错误。

struts.xml应位于目录下:src / struts.xml ..

WEB-INF / web.xml我们需要根据过滤器配置struts ..

答案 3 :(得分:1)

我遇到了同样的错误并在重新启动时查看了tomcat日志的解决方案:struts.xml中缺少doctype

所以我添加了

<xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

现在有效!

答案 4 :(得分:1)

我通过添加default-action-ref标记解决了这个问题,例如,

<default-action-ref name="home" />
包标签后的

struts.xml中的

<package name="user" namespace="/" extends="struts-default">

<default-action-ref name="upload" />

答案 5 :(得分:0)

我遇到了同样的问题,因为没有在struts.xml中正确提及命名空间,我遇到了这个问题。

我在web.xml中添加了以下代码段:

<welcome-file>login.jsp</welcome-file>

并将命名空间设为“/”默认值... 希望它能回答你的疑问..

答案 6 :(得分:-1)

在我的情况下,我不得不重命名Struts.xml ---&gt; struts.xml