我的第一个Struts2应用程序中的HTTP 404错误

时间:2015-11-09 22:05:55

标签: java jsp struts2

我已经为我的学习目的制作了一个简单的struts 2应用程序。我已经关注了youtube的教程。但是在完成我的程序后,当我运行它时,当我输入URL时显示HTTP 404错误:http://localhost:1443/Struts2Example/hello.action

以下是我的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>

以下是我的动作类

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>

以下是我的成功.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

请告诉我导致此错误的问题。

1 个答案:

答案 0 :(得分:2)

1-在您的web.xml中,您必须关闭</web-app>

2-在您的struts.xml中,您必须关闭</struts>

3-您必须添加struts 2在Eclipse中的Web部署程序集中部署路径,如此图所示 enter image description here

注意:JAVA中类名的第一个字母应该是大写字母而不是class getAction,你应该使用class GetAction或以大写字母开头的其他名称。

我使用我的配置struts 2.3.24Tomcat 8.0jdk 1.8对您的代码进行了测试,通过这些整改,它可以顺利运行。

enter image description here

在控制台

enter image description here

相关问题