Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

java - Error 404 issues using Struts application

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.

web.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>
    <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:

<%@ 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:

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:

  • commons-logging-1.1.3.jar
  • freemarker-2.3.19.jar
  • ognl-3.0.6.jar
  • struts2-core-2.3.15.1.jar
  • xwork-core-2.3.15.1.jar

Error accessing URL:

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

There are no errors on my console, problem view.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

FilterDispatcher is deprecated and may not work with the current Struts version, use StrutsPrepareAndExcecuteFilter instead.

<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.xml add the following

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

In the JSP use the form that needs to rewrite to

<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 action attribute use the action name and provide namespace as you did in the package configuration.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...