2015-11-09 77 views
0

我爲我的學習目的做了一個簡單的struts 2應用程序。我遵循YouTube上的教程。但是在完成我的程序後,當我運行它時,輸入URL時顯示HTTP 404錯誤:http://localhost:1443/Struts2Example/hello.action我的第一個Struts2應用程序中的HTTP 404錯誤

以下是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
<display-name>Struts2Example</display-name> 
<welcome-file-list> 
<welcome-file>index.html</welcome-file> 
<welcome-file>index.htm</welcome-file> 
<welcome-file>index.jsp</welcome-file> 
<welcome-file>default.html</welcome-file> 
<welcome-file>default.htm</welcome-file> 
<welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 

<filter> 
<filter-name>Struts2</filter-name> 
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    </filter> 
<filter-mapping> 
<filter-name>Struts2</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 

以下是我的Action類

package com.subir.struts2.action; 

public class getAction { 

public String execute() 
{ 

    System.out.println("Hello !execute method "); 

    System.out.println("Hello from get getAction!!"); 
    return "success"; 
} 

} 

以下是我在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"></constant> 
    <package name="default" extends="struts-default"> 
    <action name="hello" class="com.subir.struts2.action.getAction"> 
     <result name="success">/success.jsp</result> 
     <result name="error">/error.jsp</result> 
    </action> 
</package> 

以下是我的success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
Success Page!!! 
</body> 
</html> 

以下是我error.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

    <html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
Error page!!! 
</body> 
</html> 

以下是我的目錄結構。

enter image description here

請讓我知道什麼可能導致這個錯誤的問題。

+0

@AndreaLigios阿帕奇8.0.28 – Mistu4u

回答

2

1-在您web.xml你必須關閉</web-app>

2。在你的struts.xml你必須關閉</struts>

3 - 你必須在此添加Struts 2的部署路徑在Web部署大會在Eclipse中像圖片 enter image description here

注:在Java類名稱的第一個字母應該大寫字母代替class getAction你應該使用class GetAction或與UPPE啓動其他名稱rcase信。

我用我的配置struts 2.3.24,Tomcat 8.0,jdk 1.8測試了你的代碼,加上那些整改,工作沒有問題。

enter image description here

在控制檯

enter image description here

+0

您好,我錯過了'webapp'和'這裏struts'標籤,但是我用他們在我的代碼。我做的唯一更改是在部署程序集中使用「WEB_INF/lib」。感謝你的回答。 – Mistu4u

+0

@Mistu4u記得它是WEB-INF,而不是WEB_INF,以防萬一 –

+0

@AndreaLigios感謝您的評論。不過,我有一個問題:這個答案中的第3點是什麼?我不明白這一點。事實上,我沒有在我之前做過的任何一個servlet程序中使用過這種方法。 – Mistu4u

相關問題