簡體   English   中英

Struts 2 Hello World教程給出了404

[英]Struts 2 Hello World Tutorial Gives 404

我一直在關注Struts上的教程。

這是我的文件結構:

這是我的文件結構

我以為我會一步一步地遵循所有內容,我之前做過類似的教程,並且能夠在其他版本中使用它,但是我現在正在嘗試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)中遵循了該教程,並且在使用Java 1.8的Windows 10上也可以使用。

像您一樣,我不得不通過向WEB-INF / lib中添加commons-lang3-3.2.jar來避免本教程,以避免在使用Struts 2.3.33時在運行時出現NoClassDefFoundError 但是,我看到您還對web.xml進行了兩個更改,這與本教程有所不同:

  • 首先,您指定version =“ 2.5” ,應為version =“ 3.0”

  • 其次,為filter-class指定org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter而不是org.apache.struts2.dispatcher.FilterDispatcher

鑒於該教程對我來說運行正常,而無需修改它們的web.xml ,建議您還原兩個更改並重試。

是什么促使您修改其web.xml文件?


根據以下OP的評論更新了回復

好。 與我剛剛注意到的教程的另一個不同之處是,您使用Tiles 2而不是HelloWorldStruts2將項目命名為Struts2.3 我懷疑您的項目是否對HelloWorldStruts2有所了解,這將解釋您為什么得到404。

默認情況下,上下文根目錄為Struts2.3_with_Tiles_2 ,而不是HelloWorldStruts2 ,因此您的URL需要更改為http:// localhost:8080 / Struts2.3_with_Tiles_2 / index.jsp

通過選擇項目,右鍵單擊,從彈出菜單中選擇屬性 ,然后選擇Web項目設置 ,可以查看項目的上下文根 在“ 上下文根”字段中看到的所有內容都應在URL中指定。

所以要澄清一下:

  • 項目的上下文根到底是什么?
  • 究竟為URL指定了什么?

如果那不能解決問題,我建議再次運行該教程,但要嚴格按照其說明進行操作(除了需要復制的特定Struts 2文件之外,您已經在正確地進行操作了)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM