1

I'm trying to develop a Struts2 app where an action is invoked upon clicking a hyperlink which directs the user to a hello.jsp using Struts action mapping. I'm getting the following error:

HTTP Status 404 - No result defined for action com.manaar.action.HelloAction and result success

My files are as follows. My mapping looks like it's in order. I also checked other postings on here but can't seem to find the cause or solution to this problem. Would really appreciate any advice. Many thanks, J

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
    <title><s:text name="app.title" /></title>
    <link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<center>
    <h2>
        Struts 2 Actions
    </h2>
    <br>
    <br>
    Welcome
    <s:property value="#session.user" default="Guest" />!
    <s:if test="#session.user!=null">
        <s:url id="logout" action="logout" />
        | <s:a href="%{logout}">Logout</s:a> |
    </s:if>
    <br>
    <table cellspacing="5" width="180">
        <tr bgcolor="#f0edd9" height="25" align="center">
            <td>
                <s:url id="hello" action="hello"/>
                <s:a href="%{hello}">Hello Action</s:a>
                </td>
            </tr>
            <tr bgcolor="#f0edd9" height="25" align="center">
                <td>
                <s:a href="add_user.jsp">Add User</s:a>
                </td>
            </tr>
            <tr bgcolor="#f0edd9" height="25" align="center">
                <td>
                <s:a href="user.jsp">View Users</s:a>
                </td>
            </tr>
            <tr bgcolor="#f0edd9" height="25" align="center">
                <td>
                <s:a href="login.jsp">Login</s:a>
            </td>
        </tr>
    </table>
</center>
</body>
</html>

struts.xml:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
    <action name="hello" class="com.manaar.action.HelloAction" method="wateva">
        <result name="success">/hello.jsp</result>
    </action>
</package>

HelloAction.java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.manaar.action;
import com.opensymphony.xwork2.Action;
import static com.opensymphony.xwork2.Action.SUCCESS; 

public class HelloAction implements Action {

String message;

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

    /**
 *
 * @return
 * @throws Exception
 */
@Override
public String execute() throws Exception {
    setMessage("Hello From Struts!");
    return SUCCESS;
}
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
jay tai
  • 419
  • 1
  • 17
  • 35
  • 1
    try creating a method wateva in HelloAction and return SUCCESS from that method or you may call execute method in struts.xml instead of whateva – Abinash Sinha Jul 20 '13 at 04:08
  • I tried removing the method wateva completely and I got the same error. Then I tried calling execute from struts.xml. Same error – jay tai Jul 20 '13 at 13:25

2 Answers2

2

You could use a config-browser plugin. It's useful if you want to see the configuration in the browser and how actions are mapped to the URLs.

Actually, the cause of the problem that you use a convention-plugin. It's used if you put struts2-convention-plugin-2.3.x.jar into WEB-INF/lib. When installed it scans the packages, defined in the struts-plugin.xml and creates an additional to struts.xml configuration by conventions. As well as your action is comply the rules used by the plugin the action "hello" is created for the class HelloAction but unfortunately it has no a result "success". To add this result to the action you should use @Result annotation on the class, or use @ResultPath annotation to specify the path to results where they could be located instead of default WEB-INF/content. The same could be done if you apply struts.convention.result.path configuration settings.

@Result(name = SUCCESS, location = "/hello.jsp")

Also note, that the mapping you defined in the struts.xml for the action "hello" has less meaning unless it mapped to the method specified. And name of the JSP supposed a typo for index.jsp.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Many thanks for this. I am reading further into config-browser and how to use. I also removed the typo method (wateva), but I still get the same error. If I want to annotate @Result in my class, where should I put the annotation? Also what import statement will need in my class. Thank you! – jay tai Jul 20 '13 at 13:24
  • You can put it on class or on method used to map the action, or inside the `@Action` annotation in the attribute `results`, also in the `@Results` annotation body. Annotations are in the package `org.apache.struts2.convention.annotation` which comes with the convention plugin. – Roman C Jul 20 '13 at 14:06
  • Also consider reading further the second link, there's plenty of examples how to configure action mappings using annotations. – Roman C Jul 20 '13 at 14:18
  • I was able to annotate as follows: package com.manaar.action; import com.opensymphony.xwork2.Action; import static com.opensymphony.xwork2.Action.SUCCESS; import org.apache.struts2.convention.annotation.Result; @Result public class HelloAction implements Action { // REST OF THE CLASS. When I do this I get error that the annotation has no type or location paramater, but I can't seem to add this to any other part of the class – jay tai Jul 20 '13 at 14:25
  • Don't write much code in the comments, edit you post and add details after the EDIT mark. – Roman C Jul 20 '13 at 14:28
  • Roman. Thank you so much. I'm reading the second link now. Sorry about the code. I'm new here. I'll do some reading, try the annotations approach and see if this fixes the problem – jay tai Jul 20 '13 at 14:29
  • Look at this answer which has a result annotation, with the `name` and `location` attributes, you could use it. – Roman C Jul 20 '13 at 14:31
  • Also if you use static imports you could omit the `com.opensymphony.xwork2.Action` prefix, and the name attribute is not necessary, I've placed it only for comparison, by default it has a value SUCCESS. – Roman C Jul 20 '13 at 14:36
  • It worked. I added the Results annotations with location. My IDE (Netbeans) still gives action class not found errors but the app works. Many thanks for your help! – jay tai Jul 20 '13 at 22:10
  • @jaytai You should not rely on IDE, because IDE requires plugins to support framework features, but they either outdated, or not supported properly, therefore they work with errors. Anyway if it solved your problem then you should mark this answer as accepted. – Roman C Jul 21 '13 at 08:42
  • Are you saying that it's better to develop Struts2 manually (ie: to build the directory structure, compile and run from command line)? Or do you mean to ignore these error warnings when using IDE? How do I mark your answer? – jay tai Jul 22 '13 at 11:20
  • @jaytai IDE is great, but the plugins might be not so good if it warns, or errors you when it's ok, that means that the plugin is either outdated, or not supported properly. And you should decide either update plugin, rewrite it to fix bugs, not use, or configure it to suppress those errors or warnings that you thought is wrong. – Roman C Jul 22 '13 at 11:46
0

I think you missed to write the method "wateva" in HelloAction. So either write it in place of execute or remove it from your struts mapping.

<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
    <action name="hello" class="com.manaar.action.HelloAction">
        <result name="success">/hello.jsp</result>
    </action>
</package>
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49