2011-09-12 50 views
2

我正在嘗試學習Struts2,但我無法創建工作的Hello World應用程序。當從JSP發出請求時,它會發出404(頁面未找到)錯誤。我把所有的lib文件都放在了lib文件夾中,放在classes文件夾下面的struts.xml,但是我仍然收到這個錯誤。從JSP發出請求時發生404錯誤

這裏是視圖:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Hello World</title> 
    </head> 
    <body> 
    <form action="HelloWorld"> 


    <input type="submit"> 
    </form> 

    </body> 
</html> 

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <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> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
     </welcome-file-list> 
    </web-app> 

這是我struts.xml文件

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts> 
    <package name="default" extends="struts-default"> 
     <action name="HelloWorld" class="vaannila.HelloWorld"> 
      <result name="SUCCESS">/success.jsp</result> 
     </action> 
    </package> 
</struts> 

這是錯誤:

HTTP Status 404 - /Testi/HelloWorld

type Status report

message /Testi/HelloWorld

description The requested resource (/Testi/HelloWorld) is not available.

Apache Tomcat/6.0.33

+0

你怎麼做呢?您是否使用Eclipse IDE並將tomcat添加爲服務器?第一個html/jsp代碼的文件名是什麼 – Jayy

+0

這可能是因爲它沒有定位你的struts.xml,或者在啓動期間發生了一些其他錯誤,導致無法加載Struts2。 –

回答

3

這與您在Struts2 : Using form action中發佈的相同問題稍微冗長些。

你是問題出在你正在使用的窗體標籤。它不是struts表單標籤,struts表單標籤中的action屬性在HTML標籤的action屬性中處理方式不同。

我想我的回答在Struts2 : Using form action應該適合你。

PS:如果你有沒有改變你的行動延伸,你可以通過簡單地增加一個.action到你的標準HTML標記的結束動作,使這項工作:

<form action="HelloWorld.action"> 
    ... 
</form> 
0

你能發佈錯誤日誌內容嗎?以及項目的結構.404指示您的服務器能夠找到請求的資源,這可能是您映射的問題,或者可能無法在類路徑中找到映射文件。 您需要將struts.xml文件放在類路徑中,所以如果您使用的是Eclipse,只需將struts.xml文件放在src文件夾中rest eclipse知道如何處理這個問題

另外學習struts2的最好方法是下載從有官方網站的示例應用程序

Example Applications:

這將幫助你瞭解和學習struts2的更好,因爲

  1. 示例應用程序將幫助你瞭解什麼結構一個需要有
  2. 什麼所有配置文件需要放置在什麼位置
  3. 什麼是一個簡單的Hello World應用程序
1

我建議創建tomcat下一個新的文件夾中所有需要的依賴。 webapps文件夾,並根據標準放置所有文件,並嘗試使用任何IDE。從頭

嘗試調試,

  • 先看看,如果你能爲localhost擊中:成功地8080,
  • 然後嘗試打一個servlet(更改相應的web.xml中),看是否 你能夠通過。
  • 最後嘗試點擊struts2動作。確保你給的URL映射是 正確。

我認爲這應該是一個映射問題。

0

添加進口LIB到Action類

例如:

import com.opensymphony.xwork2.ActionSupport; 

public class HelloWorldAction extends ActionSupport{ 

    //.... 
    //.... 
} 
相關問題