Question

I have used Struts2 Annotation

My web.xml is:

<welcome-file-list>
    <welcome-file>index.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>

My JSP is:

      <s:form action="test" method="post">
            <s:textfield name="id" label="Id"></s:textfield>        
            <s:submit value="Submit"></s:submit>
     </s:form>

My class is:

      @Namespace("/")
      public class Test extends ActionSupport {

  private static final long serialVersionUID = 1L;

      @Action(value = "test", results = { @Result(name = "success", location =     "success.jsp") })
      public String input() {

         System.out.println("input call");
         return SUCCESS;
     }

 }

I got error:

HTTP Status 404 - There is no Action mapped for namespace / and action name test.

Folder structure:

my folder structure is

Was it helpful?

Solution

Do the following.

Place your Test action in a package called "com.mydomain.action", the first part can be what ever you want but it must end in ".action" struts2-conventions will pick up your action. If there are additional packages such as: "com.mydomain.action.here" then the packages following "action" will be interpreted as a struts2 namespace.

Please rename "success.jsp" to "test.jsp" and move it to: "/WEB-INF/content/test.jsp", /WEB-INF/content is where actions in the default namespace will have their view resolved. In the case of the /here package, views would need to be placed under /WEB-INF/content/here

Lets rewrite your Test action to keep with conventions defaults:

public class Test extends ActionSupport {
   private static final long serialVersionUID = 1L;
   public String execute() {
         System.out.println("input call");
         return SUCCESS;
   }
 }

Something else to note. You could have called your view test-success.jsp instead of just test. This is because conventions will look first for a view with the String returned from the action appended to the Action name which in this case is test.

Also while Classes are camel case: MyMagicTest a veiw for such an action class would be my-magic-test.jsp

Now where annotations fit in:

Now that you know you can do a lot without xml OR annotations... conventions makes use of annotations to OVERRIDE the defaults when needed. You do this to take shortcuts, and so annotations signal an exceptional case. Don't use them to define what conventions readily provides out of the box.

For more information on conventions please see: https://cwiki.apache.org/WW/convention-plugin.html which I would strongly recommend you review fully so you'll know many of the possible features.

OTHER TIPS

I realise this is an old question but I have recently run into this problem and solved it using the following approach so I think I will just document it here.

The way I solve it is by putting the java class - in your case Test.java - under a package named "actions" instead of leaving it in the default package.

And also remember to make the execute function public and remove struts.xml.

There is no Action mapped for namespace "xxx" and action name "yyy"

is a frequent error for beginners started to use Struts2 when configuring the action flow. To get rid of this error you need a proper mapping of URLs to the action methods.

In Struts2 definitely every action is mapped to some method. But you should differentiate the actions that explicitly mapped to the action method from the actions that implicitly mapped by default to the method execute().

There's differences in the URL formatting. If you use DMI you can map a method name in URL with exclamation sign and dispatch the method that you post to the action. Or you can use method attribute like below

<s:submit value="Submit" method="input"/>    

If you are using convention plugin and annotations make sure you doesn't have double configured your actions with XML configuration and annotations. The first one makes a precedence and overrides the configuration. So, get rid of any actions mapped in the struts.xml and apply annotations on actions, and methods, and packages.

Unfortunately, not all IDEs support Struts2 annotated configuration via plugins but you can still use double configured mapping in the separate struts-xxx.xml configurations that you can feed the IDE but not include in the main struts.xml.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top