Java HTTP 状态 404 - 没有为与上下文路径 [/struts2] 关联的命名空间 [/] 和操作名称 [login] 映射的操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20628016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login] associated with context path [/struts2]
提问by user2940073
I have learned theory of Struts2 and now practicing. Facing problems while executing project.I searched in Google in many ways but could not find result.Please help me. Below is the code.Please help me friends...
我已经学习了 Struts2 的理论,现在正在实践中。在执行项目时遇到问题。我在谷歌上搜索了很多方法都没有找到结果。请帮助我。下面是代码。请朋友帮帮我...
Project structure:
项目结构:
web.xml
网页.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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2</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>login.jsp</welcome-file>
</welcome-file-list>
struts.xml
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="resources" value="ApplicationResources" />
<constant name="struts.devMode" value="true" />
<package name="default" 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/login.jsp</result>
</action>
</package>
LoginAction.java
登录操作.java
package com.practice.structs.actions;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
private String password;
public String validateUser(){
if(this.userName.equalsIgnoreCase("abc") && this.password.equalsIgnoreCase("abc"))
{
return "success";
}else{
addActionError(getText("error.login"));
return "error";
}
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
login.jsp
登录.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Login page</title>
</head>
<body>
<H1><I>Login Page</I></H1>
<s:actionerror />
<s:form action="login.action" 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>
</body>
</html>
homepage.jsp
主页.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<H2><I>Welcome</I></H2>
</body>
</html>
回答by Lijo
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
表单中的操作名称是 action.login 并且在 struts.xml 中是 login 两者都应该相同并添加命名空间
回答by Nimmagadda Gowtham
In your LoginAction.java
properties you took are
在你的LoginAction.java
财产中,你采取的是
private String userName;
private String password;
private String userName;
private String password;
But in you login.jsp
you wrote as
但在你login.jsp
身上你写成
<s:textfield name="uname" key="label.username" size="20"/>
<s:password name="password" key="label.password" size="20"/>
<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="uname" key="label.username" size="20"/>
到
<s:textfield name="userName" key="label.username" size="20"/>
<s:textfield name="userName" key="label.username" size="20"/>
I hope this answer solves your problem ...
我希望这个答案能解决你的问题......
回答by Ryan
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:
我知道这个问题有点过时了,但我也认为值得一提,对于那些碰巧最终再次绊倒这篇文章但仍然遇到问题的人;假设您 100% 确定您的映射正确并且您的 web.xml 包含适当的过滤器,请尝试以下操作:
- Stop your Tomcat server
- Create a "classes" folder in your "WEB-INF" folder
- Move your struts.xml file into the newly created "classes" folder
- Right click on your Tomcat Server and select "Clean" - not required, but would recommend doing so.
- Start up Tomcat again and hope for the best :-)
- 停止你的 Tomcat 服务器
- 在“WEB-INF”文件夹中创建一个“classes”文件夹
- 将您的 struts.xml 文件移动到新创建的“classes”文件夹中
- 右键单击您的 Tomcat 服务器并选择“清理” - 不需要,但建议您这样做。
- 再次启动 Tomcat 并希望一切顺利:-)
As a visual aid, your WEB-INF should end up looking something like this:
作为视觉辅助,您的 WEB-INF 最终应如下所示:
If you're still experiencing the issue, double check your struts mappings again, as well as your web.xml
如果您仍然遇到问题,请再次仔细检查您的 struts 映射以及您的 web.xml
回答by AVI33
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.
我没有足够的分数来回应 Ryan 的评论,也没有给他打分,但他所说的是具体情况下的有效解决方案,我将说明原因。
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.
例如,当您使用 Eclipse 进行开发时,您可能会选择另一个项目资源管理器而不是 Eclipse 的默认“项目资源管理器”作为“导航器”。使用“导航器”视图,您可以创建文件夹,但此文件夹不是包资源(就像您使用默认的“项目资源管理器”创建“包资源”时一样),因此 Struts2 找不到“struts.xml”文件进行配置行动。因此,您的项目将作为“包资源”处理的唯一文件夹是 WEB-INF 下的文件夹,就像 Eclipse 默认在“动态 Web 项目”中所做的那样。
So then, be sure of having all the configuration files in a "package resource".
因此,请确保将所有配置文件都放在一个“包资源”中。
回答by Karthik Kompelli
make the changes on struts.xml file,, add namespace="/"
attribute in
like
<package name="default" namespace="/" extends="struts-default">
对 struts.xml 文件进行更改,namespace="/"
在 like 中添加属性
<package name="default" namespace="/" extends="struts-default">
回答by 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
为了解决这个问题,我必须在其中创建一个类文件夹WEB-INF
并将struts.xml
文件放在那里。然后我把.jsp
文件放在web文件夹中,放在文件'/'
之前.jsp
,例如/x.jsp