5

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.

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 (Action class that I'm using)

package org.koushik.javabrains.action;

public class TutorialAction {


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

Project structure

Structure of the application

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

4

6 回答 6

1

来自错误的消息清楚地表明

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

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

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

Struts 在启动时加载 xml 配置,它应该知道struts.xml. 默认情况下,它会在类路径上查找具有已知名称的文件,例如struts.xml. 然后它解析文档并创建运行时配置。此配置用于为操作 url 找到适当的配置元素。如果在请求期间没有找到这样的元素,则返回 404 错误代码和消息:There is no Action mapped for namespace and action name

此消息还包含用于查找操作配置的命名空间和操作名称。然后你可以检查你的配置设置struts.xml。有时动作名称和命名空间,存储在ActionMapping指向错误的动作。这些值ActionMapper由插件使用的可能具有不同实现的值确定。

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

于 2014-01-13T17:21:52.317 回答
1

Struts.xml将命名约定重命名为将struts.xml起作用。

于 2014-02-10T21:10:55.443 回答
0

更改Struts.xml并放入<constant name="struts.devMode" value="true" />struts.xml Development mode or devMode可以进行额外的调试并有助于调试下面的代码

   <?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>
于 2014-01-13T17:45:43.700 回答
0

你只需要检查动作名称是否在任何地方都是不变的......我也有同样的问题,我通过删除命名空间来解决,因为我看到你没有提到这不是必需的,而且我在 loginpage.jsp 和 struts 有不同的动作名称。 xml 页面.. 所以只要看看你的动作名称

于 2016-06-10T03:15:42.220 回答
-1

您的 JSP 文件不应位于 WEB-INF 文件夹下,而应位于 web 文件夹下。我附上了一张图片,向您展示了正确的层次结构。

文件夹系统

于 2015-12-30T14:58:05.870 回答
-2

我刚刚做了教程,遇到了同样的问题。事实证明,问题在于 struts.xml 文件的位置错误。确保您的 struts.xml 文件位于此处:/Struts2Starter/src/struts.xml

于 2015-03-24T23:10:32.177 回答