Struts 2 Hello World 教程给出 404

Struts 2 Hello World Tutorial Gives 404

我一直在关注 Struts 上的 this 教程。

这是我的文件结构:

我以为我一步一步地跟着一切,我之前做了一个类似的教程并且能够让它在另一个版本中工作,但我现在正在尝试 Struts 2.3.33。我在 Apache Tomcat 8.5 服务器上 运行 这个。我尝试转到 http://localhost:8080/HelloWorldStruts2/index.jsp,但它给出了 404 错误消息,我不明白为什么。我还确保 Struts 文件都在我的构建路径和我的部署程序集以及我的 WEB-INF/lib 文件夹中。

我的控制台没有错误,这是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">

   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

我的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="helloworld" extends="struts-default">

      <action name="hello" 
            class="com.tutorialspoint.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

我在你的环境中遵循了那个教程(Eclipse + Struts 2.3.33 + Tomcat 8.5.20)并且它在 Windows 10 上使用 Java 1.8 .

像你一样,我不得不偏离教程,将 commons-lang3-3.2.jar 添加到 WEB-INF/lib 以避免在使用 Struts 2.3.33 时在运行时出现 NoClassDefFoundError。但是,我看到您还对 web.xml 进行了两项偏离教程的更改:

  • 首先,您指定了 version="2.5",它应该是 version="3.0"

  • 其次,您指定了org.apache。struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 而不是 org.apache.struts2.dispatcher.FilterDispatcher 用于 filter-class.

鉴于教程对我来说没有修改他们的 web.xml 就可以很好地工作,我建议您恢复您的两个更改并重试。

是什么促使您修改他们的 web.xml 文件?


根据以下 OP 的评论更新了回复

好的。我刚刚注意到的教程的另一个偏差是您将项目命名为 Struts2.3 with Tiles 2 而不是 HelloWorldStruts2。我怀疑您的项目是否知道有关 HelloWorldStruts2 的任何信息,这可以解释为什么您会收到 404.

默认情况下,您的上下文根将为 Struts2.3_with_Tiles_2,而不是 HelloWorldStruts2,所以你的URL需要改成http://localhost:8080/Struts2.3_with_Tiles_2/index.jsp

您可以查看项目的 Context Root,方法是选择项目,右键单击,从弹出菜单中选择 Properties,然后选择Web 项目设置。无论您在 Context Root 字段中看到什么,都应该在您的 URL 中指定。

所以澄清一下:

  • 究竟什么是您项目的上下文根?
  • 您为 URL 指定的具体是什么?

如果这不能解决问题,我建议再次 运行 该教程,但请严格按照他们的说明进行操作(除了您需要复制的特定 Struts 2 个文件,但您已经正确地做到这一点)。