Struts 2基本Web应用程序404错误

时间:2015-01-29 20:16:49

标签: java eclipse tomcat struts2

这是我的设置:

规格:

Tomcat 7.0.40 jdk1.7 Eclipse Juno 赢8.1 我在Eclipse中设置了一个名为“StrutsTest”的动态Web项目,其结构如下。相关代码如下: 的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"
    version="3.0">
    <display-name>StrutsTest</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>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

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>
    <include file="struts-default.xml" />
    <package name="a" extends="struts-default">
        <action name="verify" class="com.anirban.strutstest1.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<body>

    <s:form action="verify">

        <s:textfield name="uname" label="Enter Username" />
        <br>
        <s:password name="password" label="Enter Password" />
        <br>
        <s:submit value="Click" align="center" />

    </s:form>
</body>
</html>

LoginAction.java

package com.anirban.strutstest1;

import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{
    private static final long serialVersionUID = 1L;

    private String uname,password;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

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

    public String execute()
    {
        if(uname.equals("java4s") && password.equals("pass"))
        {
            return SUCCESS;

        }else
            return ERROR;
    }
}

我尝试过FilterDispatcher而不是StrutsPrepareAndExecuteFilter。甚至将我的服务器从Tomcat改为JBoss。但仍然得到同样的错误。提前致谢。下面是文件夹结构:

Java Resources
    |_src
      |_com.anirban.strutstest1
        |_LoginAction.java
    |_resources
      |_struts.xml
    |_Libraries
Javascript Resources
build
WebContent
    |_META-INF
    |_WEB-INF
      |_lib
        |_<struts jars>
      |_web.xml
    |_<jsp files>

2 个答案:

答案 0 :(得分:1)

谢谢你的朋友们。我有答案。 Jar年纪大了。我已经取代了

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

答案 1 :(得分:0)

尝试将struts.xml中的操作更改为:

<action name="verify" class="com.anirban.strutstest1.LoginAction" method="execute">

还尝试将struts.xml放在WEB-INF / classes