2012-01-24 57 views
11

我絕對是Struts2的初學者。我正在跟隨struts網站上的教程。我跟着this tutorial。我有一些麻煩。我在eclipse上創建了動態Web項目。然後我跟着教程。但是,當我運行的例子,我得到以下錯誤。Struts Hello world示例:沒有爲命名空間[/]和動作名稱錯誤映射的動作

There is no Action mapped for namespace [/] and action name [hello] associated with context path [/Hello_World_Struts_2]. - [unknown location] 

我有以下目錄結構

enter image description here

而且我在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="basicstruts2" extends="struts-default" namespace="/"> 

    <action name="index"> 
    <result>/index.jsp</result> 
    </action> 

    <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute"> 
    <result name="SUCCESS">/HelloWorld.jsp</result> 
    </action> 

</package> 

</struts> 

感謝您的答覆。

+0

我試圖把它的代碼塊中,但我不能做到這一點。現在,我認爲沒關係。 – erencan

回答

17

struts.xml配置文件需要在類路徑上(而不是在WEB-INF)。

鏈接的教程假設一個Maven構建並指出struts.xml文件應該在src/main/resources中,該文件將包含在Maven構建的類路徑中。由於您忽略了這部分,因此您可能希望將其放在源目錄的根目錄中。

+1

非常感謝。這解決了這個問題。然而,我得到另一個錯誤「沒有結果定義的行動和成果」任何想法? – erencan

+1

@erencan是的;你的結果名稱是「SUCCESS」,但是'Action.SUCCESS'是字符串'「success」'(小寫)。這是一個猜測,因爲我不知道你的行爲是什麼樣子。另外,''execute''是默認的方法,不需要指定。 (如果您不想明確定義它,則「成功」是默認結果名稱。) –

+0

您是對的。該教程運行良好。再次感謝。 – erencan

2

您必須在WEB-INF/下創建一個classes/文件夾並將其放入您的struts.xml文件!

2

右鍵單擊該項目。轉到屬性部署構建路徑並將所有.jar文件添加到/ WEB-INF/lib。
然後你不應該得到這個錯誤。

struts.xml中應位於目錄下:SRC/struts.xml中..

和WEB-INF/web.xml中你,我們需要配置基於過濾器的支柱..

1

我同樣的錯誤,並得到解決看着重新啓動Tomcat的時候登錄:DOCTYPE是在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"> 

現在的作品!

1

我通過添加默認動作裁判標記,例如解決問題,

<default-action-ref name="home" /> 

包裝標籤後

在struts.xml中

<package name="user" namespace="/" extends="struts-default"> 

<default-action-ref name="upload" /> 
0

我有同樣的問題我正面臨它,因爲沒有在struts.xml中正確地提及名稱空間。

我添加下面的代碼片段在web.xml:

<welcome-file>login.jsp</welcome-file> 

,也使命名空間爲「/」默認值... 希望它回答您的查詢..

-1

在我而言,我不得不重新命名Struts.xml --->struts.xml

相關問題