2014-05-13 36 views
1

我是Struts的新手,我想執行HelloWorld程序,但是一旦我在web.xml中寫入<filter>標記,我就會收到「請求的資源不可用」錯誤。Struts 2中沒有的資源

Project hierarchy

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_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 

    <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.3//EN" 
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts> 
    <package name="helloworld" extends="struts-default"> 
     <action name="hello" class="sagar.practice.HelloWorldAction" method="execute"> 
      <result name="success">/HelloWorld.jsp</result> 
     </action> 
    </package> 
</struts> 

我添加了所有必需的支柱瓶當用戶庫。

我訪問索引頁爲:http://localhost:8080/practice/index.jsp

如果我刪除<filter>標籤從web.xml文件我能夠訪問index.jsp頁,但如果我添加<filter>標籤這是給「請求的資源不可用」錯誤。

+0

index.jsp是在WEB-INF下?相同的HelloWorld.jsp? –

回答

1

您應該通過在struts.xml設定恆定導通devMode。如果您想要解決諸如您的問題,請使用此模式。它更具信息性,因爲它輸出到隱藏在正常模式下的控制檯錯誤。

<constant name="struts.devMode" value="true" /> 

將它打開後,您可能會發現您的應用程序中部署了錯誤或不一致的庫集。您可能會發現一個java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils打印到控制檯,然後調整您的庫集缺少罐子。

需要commons-lang3-3.1.jar來解決錯誤。

+0

我將index.jsp中的歡迎文件更改爲web.xml中的default.jsp並嘗試過,但它仍然給出相同的錯誤。並通過刪除welcome-file-list標籤進行檢查,但沒有結果。我通過將url-pattern修改爲「/*.action」來檢查它,但遇到同樣的問題。 – Sagar

+0

你有沒有清潔構建? –

+0

我從服務器中刪除了項目並將其重新添加到服務器(也進行了清理) – Sagar

0

更改此內容在struts.xml中有以下

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 

參考鏈接:click here

如果您在使用撐杆2.0或更大的版本。使用下面的過濾器。

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
+0

更改並通過添加和刪除過濾器標記進行檢查,但它給出了相同的錯誤 – Sagar

+0

抱歉,這也沒有幫助。做這些訪問索引頁面的效果嗎?因爲我沒有調用任何操作類 – Sagar

0

執行以下操作變化的web.xml

<welcome-file>/WEB-INF/index.jsp</welcome-file> 

而在struts.xml中

<result name="success">/WEB-INF/HelloWorld.jsp</result> 

它可以幫助你。