-2

Hope someone just could give a hint where to look for the source of the problem.

Class Login Action

enter code here

@Namespace("/")
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;

@Override
@Action(value = "/welcome", results = { @Result(name = SUCCESS, location =  "/WEB-INF/content/welcome.jsp") })

When I exectute my index.jsp to execute welcome.jsp didn't work.

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>Login Page</title>
</head>
<body>
    <h3>Welcome to Struts 2</h3>
<s:form action="home">
    <s:textfield name="username" label="User Name"></s:textfield>
    <s:textfield name="password" label="Password" type="password">       </s:textfield>
    <s:submit value="Login"></s:submit>
</s:form>
</body>
</html>

welcome.jsp

enter code here

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=IUTF-8">
<title>Welcome To Struts 2</title>
</head>
<body>
   <h3>
        Welcome
       <s:property value="username"></s:property>
     !
   </h3>
</body>

Result Struts Problem Report

Struts has detected an unhandled exception:

Messages:

There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].
Roman C
  • 49,761
  • 33
  • 66
  • 176
Marcelo
  • 11
  • 1

2 Answers2

0

In Struts2 you map a form to the action config using s:form tag's action and namespace attributes.

Normally, the action attribute accepts the action name, but it can contain the URL. The form tag is rendered by the template to HTML form where the action attribute have to be URL.

So Struts is getting the action name and generate URL. You can see the source code in the browser. When request is made Struts2 uses the request URI to find the action mapping as explained here.

Usually the 404 error results in missing action configuration on the server that corresponds to the requested URI. When you submitted a form the request is made and resource is requested but the action config is not found, hence Struts2 returns 404 error.

You don't need to use annotations if you are utilizing a convention or rest plugins but the annotation always allows manually override the defaults.

By default the results path is /WEB-INF/contents/ and the action name is without slashes, so you can use annotation:

@Action(value = "welcome", results = @Result(location = "welcome.jsp") )

You can find the references to online resources from this answer.

The one from tutorials read this tutorial.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
-1

The solution.

In my index.jsp I wrote "home" in the tag action, form action="home" The correct are the same name at the Action.

<html>
    <head>
         <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
     <title>Login Page</title>
    </head>
   <body>
       <h3>Welcome to Struts 2</h3>
       <s:form action="home"> **The correct is "welcome"**
       <s:textfield name="username" label="User Name"></s:textfield>
       <s:textfield name="password" label="Password" type="password">    </s:textfield>
       <s:submit value="Login"></s:submit>
     </s:form>
   </body>
</html>

And in my LoginAction I used value ="/welcome".

package web.actions;

@Action(value = "/welcome", results = { @Result(name = SUCCESS, location = "/WEB-INF/content/welcome.jsp") })

public String execute() throws Exception {
    return SUCCESS;
}

The messagem is very clear.
There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].

But I just saw the mistake doing a lot of examples.

Marcelo
  • 11
  • 1
  • Usually on SO the question has wrong code, and the answer contains the right one. But I think you didn't write the correct one even in comments where you state its correctness. – Roman C Mar 23 '16 at 07:00
  • Also this answer looks like the edit to the question. Please distinguish the answer and the question providing a credit to the author. – Roman C Mar 25 '16 at 10:50