有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无动作映射Struts 2

下午好

我意识到这个问题已经被问了很多次,但这些问题主要是由将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>

谢谢

更新

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


共 (1) 个答案

  1. # 1 楼答案

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