本文介绍了我的第一个Struts2应用程序中的HTTP 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于学习目的,我已经做了一个简单的struts 2应用程序.我遵循了youtube上的教程.但是在完成程序后,当我运行它时,当我输入URL:http://localhost:1443/Struts2Example/hello.action时,它会显示HTTP 404错误.

I have made a simple struts 2 application for my learning purpose. I have followed the tutorial from youtube. But after completing my program, when I run it, it is showing HTTP 404 error when I enter the URL: http://localhost:1443/Struts2Example/hello.action.

以下是我的web.xml

Following is my 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

Following is my 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

Following is my 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>

以下是我的错误.jsp

Following is my 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>

以下是我的目录结构.

请让我知道导致此错误的原因是什么.

Please let me know what might be the issue causing this error.

推荐答案

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

1- In your web.xml you have to close </web-app>

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

2- In your struts.xml you have to close </struts>

3-您必须添加struts 2在Eclipse的Web Deployment Assembly中的部署路径,如图所示

3- you must add struts 2 Deploy path in Web Deployment Assembly in Eclipse like in this picture

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

NB: the first letter in class name in JAVA it should be uppercase letter instead of class getAction you should use class GetAction or other name which starts with uppercase letter.

我使用配置struts 2.3.24Tomcat 8.0jdk 1.8对您的代码进行了测试,并进行了更正,结果可以正常工作.

I tested your code with my configuration struts 2.3.24, Tomcat 8.0, jdk 1.8, with those rectification, it work without problems.

在控制台中

这篇关于我的第一个Struts2应用程序中的HTTP 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:28