Java Struts 2:没有为命名空间映射的操作 [/]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21097002/
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
Struts 2 : There is no Action mapped for namespace [/]
提问by Amila Fonseka
I am new to Struts 2 and I have been following a video tutorial on Struts 2(Koushik). I have created the Struts.xml, the action class and JSPs as same as created in the tutorial. But it gives the following exception.
我是 Struts 2 的新手,我一直在关注 Struts 2(Koushik) 的视频教程。我已经创建了 Struts.xml、动作类和 JSP,与教程中创建的一样。但它给出了以下例外。
Exception:
例外:
Jan 13, 2014 9:30:48 PM org.apache.struts2.dispatcher.Dispatcher warn
WARNING: Could not find action or result: /Struts2Starter/getTutorial.action
There is no Action mapped for namespace [/] and action name [getTutorial] associated with context path [/Struts2Starter]. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Struts.xml
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="getTutorial" class="org.koushik.javabrains.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2Starter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
TutorialAction.java (Action class that I'm using)
TutorialAction.java(我正在使用的 Action 类)
package org.koushik.javabrains.action;
public class TutorialAction {
public String execute(){
System.out.println("Hello from execute");
return "success";
}
}
Project structure
项目结构
success.jsp and error.jsp are normal jsp files with some text. I did a lot of things to resolve this issue by googling. But nothing didn't solve this. Please let me know if anyone knows what's behind this. I highly appreciate it. :)
success.jsp 和 error.jsp 是带有一些文本的普通 jsp 文件。我通过谷歌搜索做了很多事情来解决这个问题。但没有什么不能解决这个问题。如果有人知道这背后的原因,请告诉我。我非常感谢。:)
回答by Roman C
The message from error clearly says that
来自错误的消息清楚地表明
no Action mapped for namespace [/] and action name [getTutorial] associated with context path [/Struts2Starter]
没有为与上下文路径 [/Struts2Starter] 关联的命名空间 [/] 和操作名称 [getTutorial] 映射的操作
This means that action configuration is not available at runtime. Check out the config-browserplugin to debug configuration issues.
这意味着操作配置在运行时不可用。查看config-browser插件以调试配置问题。
To map correctly url to the action two parameters are required: the action name and namespace.
要将 url 正确映射到操作,需要两个参数:操作名称和命名空间。
Struts loads xml configuration on startup and it should know the location of the struts.xml
. By default it's looking on classpath to find a file with known name like struts.xml
. Then it parses document and creates a runtime configuration. This configuration is used to find appropriate configuration element for the action url. If no such element is found during request, the 404 error code is returned with the message: There is no Action mapped for namespace and action name
.
Struts 在启动时加载 xml 配置,它应该知道 .xml 文件的位置struts.xml
。默认情况下,它会在类路径上查找具有已知名称的文件,例如struts.xml
. 然后它解析文档并创建运行时配置。此配置用于为操作 url 查找适当的配置元素。如果在请求期间没有找到这样的元素,则返回 404 错误代码和消息:There is no Action mapped for namespace and action name
。
Also this message contains a namespace and action names used to find the action config. Then you can check your configuration settings in struts.xml
. Sometimes the action name and namespace, stored in ActionMapping
point to the wrong action. These values are determined by the ActionMapper
which could have different implementation, used by plugins.
此消息还包含用于查找操作配置的名称空间和操作名称。然后您可以在struts.xml
. 有时动作名称和命名空间,存储在ActionMapping
指向错误动作的地方。这些值ActionMapper
由插件使用的可能具有不同实现的决定。
There's also another setting that might affect this mapper and mapping, but the point is the same if you get this message then URL is used didn't map any action config in runtime configuration. If you can't realize which URL you should use, you might try config-browserplugin to see the list of URLs available to use.
还有另一个设置可能会影响此映射器和映射,但如果您收到此消息,则使用的 URL 没有映射运行时配置中的任何操作配置,这一点是相同的。如果您不知道应该使用哪个 URL,您可以尝试使用config-browser插件来查看可用的 URL 列表。
回答by Raju Rathore
change your Struts.xml
and put <constant name="struts.devMode" value="true" />
in struts.xml
Development mode or devMode
it enables extra debugging and helps indebugging
try below code
改变你Struts.xml
,并把<constant name="struts.devMode" value="true" />
在struts.xml
Development mode or devMode
它使额外的调试和帮助下面的代码indebugging试
<?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>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="getTutorial" class="org.koushik.javabrains.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
回答by balaji
Renaming Struts.xml
naming convention to struts.xml
will work.
重Struts.xml
命名命名约定struts.xml
将起作用。
回答by tony huynh
I just did the tutorial and had the same problem. It turns out the problem was the struts.xml file was in the wrong location. Make sure your struts.xml file is located here: /Struts2Starter/src/struts.xml
我刚做了教程,遇到了同样的问题。原来问题是 struts.xml 文件位于错误的位置。确保您的 struts.xml 文件位于此处:/Struts2Starter/src/struts.xml
回答by u5731106
Your JSP file should not be under the WEB-INF folder, but should be under the web folder. I've attached an image to show you the right hierarchy.
您的 JSP 文件不应在 WEB-INF 文件夹下,而应在 web 文件夹下。我附上了一张图片来向您展示正确的层次结构。
回答by shashi mishra
U just need to check that action name is constant everywhere...i also had same problem i resolved by removing namespace as it is not necessary which i see u have not mentioned and also i had different action name at loginpage.jsp and struts.xml page.. so just see ur action name
你只需要检查动作名称在任何地方都是不变的......我也有同样的问题,我通过删除命名空间解决了同样的问题,因为它不是必需的,我看到你没有提到,而且我在 loginpage.jsp 和 struts 上有不同的动作名称。 xml 页面 .. 所以只要看看你的动作名称