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

那年仲夏 提交于 2019-12-06 12:03:44

change your code like this

 <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="com.practice.structs.actions.LoginAction"
            method="validateUser">
            <result name="success">pages/homepage.jsp</result>
            <result name="error">pages/![enter image description here][1]login.jsp</result>
        </action>

    </package>


<s:form action="login" method="post">
    <s:textfield name="uname" 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>

your action name in form is action.login and in struts.xml is login both should be same and also add the namespace

I know this question is a bit outdated, but I also thought it's worth mentioning, to those who happen to end up stumbling onto this post again and still experiencing the issue; assuming you're 100% sure that your mappings are correct and that your web.xml contains the appropriate filter, try the following:

  1. Stop your Tomcat server
  2. Create a "classes" folder in your "WEB-INF" folder
  3. Move your struts.xml file into the newly created "classes" folder
  4. Right click on your Tomcat Server and select "Clean" - not required, but would recommend doing so.
  5. Start up Tomcat again and hope for the best :-)

As a visual aid, your WEB-INF should end up looking something like this:

If you're still experiencing the issue, double check your struts mappings again, as well as your web.xml

I don't have enough points to respond Ryan's comment nor rate him, but what he says is a valid solution in concrete cases, and I am going to tell why.

Sometimes the folders you create in a project are not taken as resources of the application, and you have to configure it.

Let me explain myself with a practical example that may have occurred to some mates who asked for this problem:

When you are developing with Eclipse, maybe you choose another project explorer than the Eclipse's default "Project Explorer", as the "Navigator", for example. Using "Navigator" view, you can create folders, but this folders are not package resources (as they are when you create "package resources" with the default "Project Explorer"), so Struts2 cannot find "struts.xml" file to configure the actions. So, the only folders that your project will process as "package resources" are the ones under WEB-INF as Eclipse do in "Dynamic Web Projects" by default.

So then, be sure of having all the configuration files in a "package resource".

In your LoginAction.java properties you took are

private String userName; private String password;

But in you login.jsp you wrote as

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

Change

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

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

I hope this answer solves your problem ...

make the changes on struts.xml file,, add namespace="/" attribute in like <package name="default" namespace="/" extends="struts-default">

JO1234

To solve this problem I had to create a classes folder in WEB-INF and place the struts.xml file there. Then I placed the .jsp files in the web folder and placed '/' before the .jsp files, for example /x.jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!