Struts 2:没有为命名空间映射的操作 [/] [英] Struts 2 : There is no Action mapped for namespace [/]

查看:34
本文介绍了Struts 2:没有为命名空间映射的操作 [/]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Struts 2 的新手,我一直在关注 Struts 2(Koushik) 的视频教程.我已经创建了 Struts.xml、动作类和 JSP,与教程中创建的一样.但它给出了以下异常.

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.

例外:

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

<?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 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(我使用的动作类)

package org.koushik.javabrains.action;

public class TutorialAction {


    public String execute(){
        System.out.println("Hello from execute");
        return "success";
    }
}

项目结构

success.jsp 和 error.jsp 是带有一些文本的普通 jsp 文件.我通过谷歌搜索做了很多事情来解决这个问题.但没有什么不能解决这个问题.如果有人知道这背后的原因,请告诉我.我非常感谢.:)

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. :)

推荐答案

错误消息中明确指出

没有为命名空间 [/] 和操作名称 [getTutorial] 映射的操作关联上下文路径[/Struts2Starter]

no Action mapped for namespace [/] and action name [getTutorial] associated with context path [/Struts2Starter]

这意味着操作配置在运行时不可用.查看 config-browser 插件以调试配置问题.

This means that action configuration is not available at runtime. Check out the config-browser plugin to debug configuration issues.

要将 url 正确映射到操作,需要两个参数:操作名称和命名空间.

To map correctly url to the action two parameters are required: the action name and namespace.

Struts 在启动时加载 xml 配置,它应该知道 struts.xml 的位置.默认情况下,它在类路径上查找具有已知名称的文件,例如 struts.xml.然后它解析文档并创建运行时配置.此配置用于为操作 url 查找适当的配置元素.如果在请求过程中没有找到这样的元素,则返回 404 错误代码和消息:没有为命名空间和操作名称映射的操作.

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 中检查您的配置设置.有时动作名称和命名空间,存储在 ActionMapping 指向错误的动作.这些值由 确定ActionMapper 可以有不同的实现,由插件使用.

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.

还有另一个设置可能会影响此映射器和映射,但如果您收到此消息,则使用的 URL 没有映射运行时配置中的任何操作配置,这一点是相同的.如果您不知道应该使用哪个 URL,您可以尝试 config-browser 插件来查看可用的 URL 列表.

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-browser plugin to see the list of URLs available to use.

这篇关于Struts 2:没有为命名空间映射的操作 [/]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆