-->

There is no Action mapped for namespace / and acti

2020-05-01 08:34发布

问题:

I am unable to execute my struts2 application. I am using eclipse indigo IDE, tomcat 7 and jdk 1.7.

The jar files I included are:

  • commons-logging-1.0.4.jar,
  • freemarker-2.3.8.jar,
  • ognl- 2.6.11.jar,
  • struts2-core-2.0.11.jar,
  • xwork-2.0.4.jar

I placed the struts.xml in classes folder in WEB-INF and I also tried it placing in
src folder but I could not able to make it. I am getting the below error on console

There is no Action mapped for namespace / and action name tutorial. - [unknown    location]

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   
"http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
    <title>Insert title here</title>  
</head>  
<body>  
    <form action="./tutorial.action">  
        Username: <input type="text" />  
        <input type="submit" value="Submit" />  
    </form>  
</body>  
</html>  

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="tutorial" class="com.test.TutorialAction">
            <result name="success">/success.jsp</result>
            <result name="failure">/failure.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"   
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_3_0.xsd" id="WebApp_ID" version="3.0">

    <display-name>Struts2Starter</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

TutorialAction.java

package com.test;   

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

回答1:

As others have pointed out, you do not have a getTutorial action in your mapping, only a "tutorial". However, I'm going to skip over that and suggest that you learn how to sanity check a Struts2 app. Anytime you are setting up a new technology, and even when you are working with a familiar technology, it is very useful to understand how to do a basic sanity check -- in this case, you need to verify that your struts xml has been successfully parsed and the framework knows about your actions.

I HIGHLY RECOMMEND you add the Struts 2 Config Browser plugin to your struts 2 apps. To add the plugin, you just get the jar ( all struts 2 plugins are jars ) and put in your webapp's lib directory. That's it. When you start your applicaiton, hit this URL:

http://localhost:8080/starter/config-browser/index.action

And it shows you all of the actions, as well as other configurations, that the framework knows about. Essentialy struts 2 diagnostic tool, and too easy to use to not use it.



回答2:

Try to add namespace attribute into you package element of struts.xml file.

Like this:

<struts>
<package name="default" extends="struts-default" namespace="/">
    <action name="tutorial" class="com.test.TutorialAction">
        <result name="success">/success.jsp</result>
        <result name="failure">/failure.jsp</result>
    </action>
</package>
</struts>


回答3:

I had the same problem and I saw multiple posts on this one....here is the possible solution

I was pretty sure that I had mapped all my actions accurately, but it was showing the same error above....so I just cleaned the project and then ran it again..it worked perfectly fine...give it a try !

I encountered this so many times...so to avoid such kind of things, I just added "../eclipse.exe -clean" to the shortcut icon property....this works and u can forget about getting such kind of errors which is actually not an error....!



回答4:

The error comes because the server is not able to find correct path for struts.xml . Its better to put the struts.xml in parallel to src folder or in WEB-INF/classes.



回答5:

Try this once don't append .action

<form action="tutorial">

place your struts.xml file in src outside the package or in WEB-INF classes folder.