Java脑洞struts2项目不是运行虽然代码和教程一样

Java Brains struts 2 project is not running though the code is same as the tutorial

我正在从 Java Brains 学习 struts 2。我编写了一个简单的 Hello world 类型 struts 2 网络应用程序。它只显示成功页面或错误页面。我已完成教程中指示的所有操作,但不幸的是页面未加载。以下是项目浏览器。

我编辑了 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts 2 Web Application</display-name>
    <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>
</web-app>

根据教程,上面的web.xml配置将整个请求响应过程交给了struts。我已经配置了 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="default" extends="struts-default">
        <action name="getTutorial" class="action.TutorialAction">
            <result name="success">/success.jsp</result>
            <result name="failure">/error.jsp</result>
        </action>
    </package>
</struts>

我写了 action.TutorialAction class 如下。

package action;

public class TutorialAction {
    public String execute(){
        System.out.println("Hello From execute");
        return "success";
    }
}

在 运行 Tomcat 7 下的项目之后,url“http://localhost:8080/Struts2Starter/getTutorial.action”应该加载 success.jsp 页面。但是它显示 404 错误。

success.jsp 页面只是一个 .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>

我不知道为什么不是 运行。我一直在努力解决一个星期,但最后还是没有希望:(。请帮助我,在此先感谢。

请仔细看以下几点:

1) 您必须添加 struts2 的所有 jar 文件,这些文件在 "javatpoint".

上很容易获得

2) 运行 它只用 getTutorial 而不是 getTutorial.action,就像“http://localhost:8080/Struts2Starter/getTutorial”有时它的工作。

希望它对你有用。

Java Brains uses struts 2 Version 2.3.1.1. But the version of struts used in this project is 2.3.24. These two versions conflict. The project runs successfully using version 2.3.1.1. You can download struts 2.3.1.1 or any other older version of struts from here.

同样的问题-我下载了 struts-2.3.2,这是 link http://archive.apache.org/dist/struts/ but still comes error 404-not found with the above specified urls i.e http://localhost:8080/Struts2Starter/getTutorial 中提供的 struts2 中最旧的版本 和 http://localhost:8080/Struts2Starter/getTutorial.action 两者。

请仔细看以下几点:

1) 您必须添加 struts2 的所有 jar 文件,这些文件在 "javatpoint".

上很容易获得

2) 运行 它只用 getTutorial 而不是 getTutorial.action,就像“http://localhost:8080/Struts2Starter/getTutorial”有时它的工作。 您还可以从此处观看 Java 视频 https://www.youtube.com/channel/UCWJ9QB_Ng-dgEOFd_1Ouobg