没有动作映射 - Struts 2 [英] No action mapped - Struts 2

查看:67
本文介绍了没有动作映射 - Struts 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,

我意识到这个问题已经被问过很多次了,但这些问题主要是由于将 struts.xml 文件放在不正确的位置或拼错了路径名之类的人造成的.

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

HTTP 状态 404 - 没有为与上下文路径 [/basic_struts] 关联的命名空间 [/] 和操作名称 [hello] 映射的操作.

  • 我使用 Maven 作为我选择的构建工具
  • 我的 struts.xml 文件位于 WebContent 文件夹的根目录中,不是WEB-INF\类 文件夹.

以下是我的 struts.xml 文件的结构:

<支柱><constant name="struts.devMode" value="true"/><package name="default" namespace="/" extends="struts-default"><!-- 这个最小的 Struts 2 配置文件告诉框架如果 URL 以 index.action 结尾,则将浏览器重定向到 index.jsp.--><动作名称=索引"><result>/index.jsp</result></动作><动作名称=你好"class="com.me.actions.HelloWorldAction" method="execute"><result name="success">jsp/HelloWorld.jsp</result></动作></包>

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

<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 文件中包含一个依赖项 - 在我的文件中,它看起来像这样:

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

谢谢

更新

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

解决方案

看起来像是对错误的简要说明:struts.xml 文件应该在 Web 应用程序类路径上.src/main/resources 对应于 Maven 项目结构,应该作为源文件夹添加到您的 web 项目中.构建时,它与其他源文件夹合并,这些文件夹被编译并与编译类一起放置到编译器输出的文件夹中.部署时编译器输出文件夹复制到 WEB-INF/classes.请参阅 Maven 站点以了解 Web 项目入门.

Good afternoon,

I realise this question has been asked multiple times but these questions were mostly caused by the person placing the struts.xml file in the incorrect place or mis-spelling a path name or something.

I've followed the tutorial from the Struts 2 website here as best I can but keep getting the following error:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [hello] associated with context path [/basic_struts].

  • I'm using Maven as my build tool of choice
  • My struts.xml file is located in the root of the WebContent folder and is not in the WEB-INF\classes folder.

Below is the structure of my struts.xml file:

<?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>

I have also placed the HelloWorld.jsp file in a folder called jsp and I've pointed this out in the struts file. The index file code which is suppose to call the above action looks like this:

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

Questions

1) Is my placing of the struts.xml file correct? (note - my web.xml file is inside the WEB-INF folder) and if not where should it be placed?

(note - i read it should be placed in the src/main/resources folder but where is that? I would have thought that was part of the java resources but why place it there? )

2) Do you need to include any jars in the lib folder or build path for this to work? From the website all you have to do is include a dependency in the Maven pom file - in mine this looks like this:

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

Thanks

Update

Thanks to Roman C for his help. My struts file was in the wrong place. If anyone else has this problem I would recommend checking out this question that has a nice screenshot of where to position the struts file - please remember to call it struts.xml (lowercase) instead of uppercase.

解决方案

Looks like a brief explanation of error: the struts.xml file should be on web application classpath. src/main/resources correspond to the Maven project structure and should be added as a source folder to your web project. When built it's merged with other source folders that are compiled and placed to the folder of the compiler output along with compiled classes. When deployed the compiler output folder copied to WEB-INF/classes. See the Maven site for getting started with web projects.

这篇关于没有动作映射 - Struts 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆