简体   繁体   中英

No Action mapped for namespace [/] and action name

I'm getting this error:

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>

The folder structure is :

在此处输入图片说明

EDIT:

Action code:

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);
    }
}

You are missing namespace="/" in package tag of struts.xml file

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

According to the struts dtd the order of the elements in the configuration file matters. Try

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

the option to select full namespace taken back to default, there's no reason to change this constant.

and remove

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

the struts is unable to find an action because welcome files served first by the web server.

Now, edit the struts.xml and add the following in your package

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

So, when you go to the web app root path it will execute default action and redirect to the action loginForm .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM