【问题标题】:404 error on java web app with tomcat, struts2, and eclipse使用 tomcat、struts2 和 eclipse 的 java web 应用程序出现 404 错误
【发布时间】:2015-12-10 19:54:28
【问题描述】:

我正在构建我的第一个 struts2 应用程序并收到错误:

  • HTTP 状态 404 - /Struts2Beginner/Input.jsp
  • 类型状态报告 - 消息 /Struts2Beginner/Input.jsp 描述请求的资源 不可用。
  • Apache Tomcat/7.0.64

关于这个问题的帖子很多,我想我消除了两个常见问题:

  1. 我正在使用 StrutsPrepareAndExecuteFilter(而不是 FilterDispatcher)
  2. 我的 WEB-INF/lib 中有 commons-lang-2.4.jar 和 commons-lang3-3.2.jar

我正在关注教程http://www.codejava.net/frameworks/struts/struts2-beginner-tutorial-eclipse-tomcat-xml?showall=&limitstart=

WEB-INF 库

  • commons-fileupload-1.2.2.jar
  • commons-io-2.0.1.jar
  • commons-lang-2.4.jar
  • commons-lang3-3.1.jar
  • commons-logging-1.1.1.jar
  • commons-logging-api-1.1.jar
  • freemarker-2.3.19.jar
  • javassist-3.11.0.GA.jar
  • ognl-3.0.6.jar
  • struts2-core-2.3.8.jar
  • xwork-core-2.3.8.jar

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>
    <package name="Struts2Beginner" extends="struts-default">
        <action name="calculateSumAction" class="net.codejava.struts.SumAction"
            method="calculate">
            <result name="success">Result.jsp</result>
            <result name="input">Input.jsp</result>
        </action>
    </package>
</struts>

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"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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>Struts2Beginner</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>

SumAction.java

package net.codejava.struts;

import com.opensymphony.xwork2.ActionSupport;

public class SumAction extends ActionSupport {
    private int x;
    private int y;
    private int sum;

    /**
     * The action method
     * @return name of view
     */
    public String calculate() {
        sum = x + y;
        return SUCCESS;
    }

    // setters and getters for x, y, and sum:

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }
}

Input.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ 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>Struts2 beginner example application</title>
</head>
<body>
    <center>
        <h2>Calculate sum of two numbers</h2>
        <s:form action="calculateSumAction" method="post">
            <s:textfield name="x" size="10" label="Enter X" />
            <s:textfield name="y" size="10" label="Enter Y" />
            <s:submit value="Calculate" />
        </s:form>
    </center>
</body>
</html>

Result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ 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>Sum Result</title>
</head>
<body>
    Sum of <s:property value="x"/>
    and <s:property value="y"/>
    is:
    <s:property value="sum"/>
</body>
</html>

到目前为止,我已经花了几个小时尝试调试它,但没有成功。

【问题讨论】:

标签: java eclipse jsp tomcat struts2


【解决方案1】:

当 web.xml 中没有欢迎文件列表时将面临 404 错误。

在 web.xml 中添加以下代码,

<welcome-file-list>
  <welcome-file>Input.jsp</welcome-file></welcome-file-list>

并通过右键单击运行项目 -> 运行方式 -> 在服务器上运行。并让我们知道您是否能够运行该应用程序。我已经在我的本地 tomcat 服务器上尝试过这个程序,它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多