【问题标题】:No action mapped - Struts 2没有映射动作 - Struts 2
【发布时间】:2014-05-07 15:34:28
【问题描述】:

下午好,

我知道这个问题已被多次问过,但这些问题主要是由于将struts.xml 文件放在不正确的位置或拼写错误的路径名或其他内容的人造成的。

我已尽我所能遵循 Struts 2 网站 here 的教程,但不断收到以下错误:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [hello] associated with context path [/basic_struts].
  • 我选择使用 Maven 作为构建工具
  • 我的struts.xml 文件位于WebContent 文件夹的根目录中,不在WEB-INF\classes 文件夹中。

下面是我的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="default" namespace="/" extends="struts-default">
        <!-- This minimal Struts 2 configuration file tells the framework that 
            if the URL ends in index.action to redirect the browser to index.jsp. -->
        <action name="index">
            <result>/index.jsp</result>
        </action>

        <action name="hello"
            class="com.me.actions.HelloWorldAction" method="execute">
            <result name="success">jsp/HelloWorld.jsp</result>
        </action>
</package>

我还将HelloWorld.jsp 文件放在一个名为jsp 的文件夹中,并且我已经在struts 文件中指出了这一点。假设调用上述操作的索引文件代码如下所示:

<p>
    <a href="<s:url action='hello'/>">Hello World</a>
</p>

问题

1) 我对struts.xml 文件的放置是否正确? (注意 - 我的 web.xml 文件在 WEB-INF 文件夹内)如果不是,它应该放在哪里?

(注意 - 我读到它应该放在 src/main/resources 文件夹中,但那在哪里?我原以为这是 java 资源的一部分,但为什么要把它放在那里?)

2) 您是否需要在 lib 文件夹或构建路径中包含任何 jar 才能使其正常工作?从网站上,您所要做的就是在 Maven pom 文件中包含一个依赖项 - 在我的文件中,这看起来像这样:

    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.16</version>
    </dependency>

谢谢

更新

感谢 Roman C 的帮助。我的 struts 文件放错了地方。如果其他人有这个问题,我建议您查看这个question,它有一个很好的屏幕截图,显示了 struts 文件的位置 - 请记住将其命名为 struts.xml(小写)而不是大写。

【问题讨论】:

    标签: java maven struts2 action-mapping


    【解决方案1】:

    看起来像是对错误的简要说明:struts.xml 文件应位于 Web 应用程序类路径中。 src/main/resources 对应于 Maven 项目结构,应作为源文件夹添加到您的 Web 项目中。构建时,它与其他已编译的源文件夹合并,并与已编译的类一起放置到编译器输出的文件夹中。部署时将编译器输出文件夹复制到WEB-INF/classes。请参阅Maven site for getting started with web projects

    【讨论】:

    • 那么我对 struts 文件的定位是正确的还是需要移动它?
    • @Katana24 ...您的 struts.xml 在 WEB-INF 中。 WEB-INF 不在类路径中。显然,您需要移动它,以遵循文档和所有其他 S2 对错位 struts.xml 的回答。还有你正在学习的教程。
    • @Katana24 将其移至src/main/resourcessrc/main/java
    • @RomanC 我知道 - 我用答案更新了我的问题并链接到我找到答案的地方
    • @Katana24 如果链接到问题答案的这个和那个对你有帮助,那么你应该接受并支持他们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多