2015-11-09 75 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

相关问题