0

我收到此错误:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [loginForm] associated with context path

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <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>
   <package name="default" extends="struts-default"> 
        <action name="loginForm" class="org.nitish.action.LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="failure">index.jsp</result>
        </action>
    </package>
    <bean name="loginForm" class="org.nitish.form.LoginForm"></bean>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />
</struts>

index.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Login page | Hello World Struts application in Eclipse</title>
    </head>
    <body>
    <h1>Login</h1>
    <s:form action="loginForm">
        <s:textfield name="userName" label="Username" />
        <s:password name="password" label="Password" />
        <s:submit />
    </s:form>
    </body>
</html>

文件夹结构为:

在此处输入图像描述

编辑:

动作代码:

public class LoginAction extends ActionSupport {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String target = null;
        LoginForm loginForm = (LoginForm)form; 

        if(loginForm.getUserName().equals("admin")
                && loginForm.getPassword().equals("admin123")) {
            target = "success";
            request.setAttribute("message", loginForm.getPassword());
        }
        else {
            target = "failure";
        }

        return mapping.findForward(target);
    }
}
4

2 回答 2

0

您在文件namespace="/" 的包标签中丢失struts.xml

<package name="default" extends="struts-default" namespace="/"> 
    <action name="loginForm" class="org.nitish.action.LoginAction">
        <result name="success">Welcome.jsp</result>
        <result name="failure">index.jsp</result>
    </action>
</package> 
于 2013-09-02T10:19:26.637 回答
0

根据struts dtd,配置文件中元素的顺序很重要。尝试

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />

    <bean name="loginForm" class="org.nitish.form.LoginForm"></bean>

   <package name="default" extends="struts-default"> 
        <action name="loginForm" class="org.nitish.action.LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="failure">index.jsp</result>
        </action>
    </package>
</struts>

选择完整命名空间的选项恢复为默认值,没有理由更改此常量。

并删除

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

struts 无法找到动作,因为欢迎文件首先由 Web 服务器提供。

现在,编辑struts.xml并在您的包中添加以下内容

<action name=""> 
  <result type="redirectAction">loginForm</result> 
</action>

因此,当您转到 Web 应用程序根路径时,它将执行默认操作并重定向到该操作loginForm

于 2013-09-02T10:23:38.233 回答