Java 使用 Struts 应用程序时出现错误 404 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18419661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:28:55  来源:igfitidea点击:

Error 404 issues using Struts application

javajspstruts2actiondmi

提问by Vimal Basdeo

I am having some issues running a Struts web app since few days. I tried several solutions from StackOverflow relating to my problem but none of them works.

几天以来,我在运行 Struts Web 应用程序时遇到了一些问题。我尝试了 StackOverflow 中与我的问题相关的几种解决方案,但没有一个有效。

web.xml

网页.xml

<display-name>Struts2 Application</display-name>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>

        org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>

</filter-mapping>
<welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
</welcome-file-list>

struts.xml

struts.xml

<struts>
    <constant name="struts.enable.DynamicMethodInvocation"
        value="false" />
    <constant name="struts.devMode" value="false" />

    <constant name="struts.custom.i18n.resources"
        value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">

        <action name="login"
            class="net.viralpatel.struts2.LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>

        </action>
    </package>
</struts>

Login.jsp

登录.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>

<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>

<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login.action" method="post">

    <s:textfield name="username" key="label.username" size="20" />
    <s:password name="password" key="label.password" size="20" />

    <s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>

LoginAction.java

登录操作.java

package net.viralpatel.struts2;

public class LoginAction {
    private String username;
    private String password;

    public String execute() {

        if (this.username.equals("admin")
                && this.password.equals("admin123")) {
            return "success";
        } else {
            return "error";
        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Libs I have added

我添加的库

1.commons-logging-1.1.3.jar
2. freemarker-2.3.19.jar
3. ognl-3.0.6.jar
4. struts2-core-2.3.15.1.jar
5. xwork-core-2.3.15.1.jar

Error accessing url http://localhost:8080/StrutsHelloWorld/

访问网址时出错 http://localhost:8080/StrutsHelloWorld/

HTTP Status 404 - /StrutsHelloWorld/

type Status report

message /StrutsHelloWorld/

description The requested resource is not available.

Apache Tomcat/7.0.42

I tried this http://java.dzone.com/articles/struts2-tutorial-part-27

我试过这个http://java.dzone.com/articles/struts2-tutorial-part-27

There are no errors on my console, problem view. I would be glad if someone could help me.

我的控制台上没有错误,问题视图。如果有人可以帮助我,我会很高兴。

采纳答案by Roman C

The error 404 means that resource you have requested is not available, the same referred to the action that is didn't map to the request URL.

错误 404 表示您请求的资源不可用,同样指的是未映射到请求 URL 的操作。

FilterDispatcheris deprecated and may not work with the current Struts version, use StrutsPrepareAndExcecuteFilterinstead.

FilterDispatcher已弃用,可能不适用于当前的 Struts 版本,请StrutsPrepareAndExcecuteFilter改用。

<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>

in the struts.xmladd the following

struts.xml添加以下内容

<package name="default" extends="struts-default" namespace="/">
   <action name=""><result>/Login.jsp</result></acton> 

this will forward you to the login page. Also consider to use absolute paths to the JSP resources.

这会将您转到登录页面。还要考虑使用 JSP 资源的绝对路径。

In the JSP use the form that needs to rewrite to

在JSP中使用需要重写的表单

<s:form namespace="/" action="login" method="post">

    <s:textfield name="username" key="label.username" size="20" />
    <s:password name="password" key="label.password" size="20" />

    <s:submit key="label.login" align="center" />
</s:form>

In the actionattribute use the action name and provide namespaceas you did in the package configuration.

action属性中使用操作名称并提供namespace您在包配置中所做的。

Using method="execute"is useless in the s:submittag, the method executeused by default, and it will not work because you have turned off DMI in the Struts configuration.

method="execute"s:submit标签中使用是没用的,execute默认使用的方法,不会生效,因为你在Struts配置中关闭了DMI。

Addinglibraries over the old libraries in the WEB-INF/libdoesn't help so much and your application probably would not work, until you remove and replaceall lower version libraries with higher version and add new libraries needed by the current version of Struts framework.

在旧库上添加WEB-INF/lib并没有太大帮助,您的应用程序可能无法运行,直到您删除所有较低版本的库并将其替换为较高版本并添加当前版本的 Struts 框架所需的新库。