【问题标题】:HTTP Status 404 on Struts project with tomcat带有tomcat的Struts项目上的HTTP状态404
【发布时间】:2016-10-20 20:03:30
【问题描述】:

我正在尝试关注this 教程,了解如何开始使用 Struts。 我遵循了确切的步骤,但最后我得到了这个错误HTTP Status 404 - /Struts2-HelloWorldExample/

我知道 404 错误意味着我能够与服务器通信,但服务器找不到我请求的内容。我怀疑我的库有问题,但我在解决同一问题的解决方案中找不到任何东西。

我的java代码:

package com.aziz;

public class HelloWorldExample {

    private String name;

    public String execute() throws Exception {
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  <display-name>Struts 2 - Hello World Example</display-name>
  <welcome-file-list>
     <welcome-file>index.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>
</web-app>

你好.jsp:

<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
    <title>Hello World Example</title>
</head>
<body>
    Hello <s:property value="name"/>!
</body>
</html>

index.jsp:

<!DOCTYPE html>
<html>
<head>
    <title>Hello World Example</title>
</head>
<body>
    <h1>Hello World Example</h1>
    <form action="hello">
        <label for="name">Enter your name</label><br/>
        <input type="text" name="name"/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>

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" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello"
                class="com.aziz.HelloWorldExample">
                <result name="success">hello.jsp</result>
        </action>
    </package>
</struts>

我不确定我在做什么是对还是错,但我只是手动添加了我从here(完整发行版)下载的 WEB-INF/lib 文件夹中的 struts 库,所以我有该文件夹中的这些 JAR:

最后一件事,我没有错误,但我的标记上有这些警告:

我已经检查了这个错误,我知道这是什么意思,因为当我看到解决方案时,有人说我需要决定是否以及将哪些库复制到项目属性中的 WAR -> Java EE 模块依赖项。但是在我的项目属性中,没有 Java EE Module Dependencies 之类的东西。 我真的需要你的帮助。 提前致谢。

【问题讨论】:

  • 尝试修改这个 -> /WebContent/index.jsp
  • 不要包含完整发行版中的所有 jar。如果您不知道要包含什么内容,请下载 Essential Dependencies Only 分发版。
  • 您可能在 Eclipse 中有错误的项目配置和错误的依赖项。使用 Maven 重新创建一个项目。

标签: java jsp jakarta-ee struts2 http-status-code-404


【解决方案1】:

确保您已将 jsp 文件放入 WEBCONTENT 而不是 WEB-INF 中,并尝试仅放入我在下面给出的库。

commons-collections-3.1.jar, commons-fileupload-1.3.1.jar, commons-io-2.2.jar, commons-lang-2.4.ja,r commons-lang3-3.2.jar, commons-logging-1.1.3.jar, freemarker-2.3.22.jar, javassist-3.11.0.GA.jar, ognl-3.0.13.jar, struts2-core-2.3.28.jar, xwork-core-2.3.28.jar,

试试这个并尝试重新启动服务器并检查它。希望它有效。

问候, 禅雅

【讨论】:

  • 为什么你认为把文件放到web-inf是不对的?
  • @AleksandrM: 无法直接访问 WEB-INF 文件夹中的内容,因此您将无法调用在 WEB-INF 文件夹中创建的 jsp 文件。您只需尝试创建您的WEB-INF 之外的 jsp 文件。
  • 为什么需要直接访问?动作 -> JSP。不是其他方式。
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 2018-11-09
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多