I’m using Struts2. I have two web forms that have the same code. I would like to eliminate one form. Here is the structure of my Struts project.
\Web Pages
form.jsp
\WEB-INF
\Content
error.jsp
form.jsp
success.jsp
\Source Packages
\action
MyAction.java
MyAction.java
package action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;
public class MyAction extends ActionSupport {
@Action(value = "foo", results = {
@Result(name = "input", location = "form.jsp"),
@Result(name = "success", location = "success.jsp"),
@Result(name = "error", location = "error.jsp")
})
public String execute() throws Exception {
if (user.length() == 1) {
return "success";
} else {
return "error";
}
}
private String user = "";
public void validate() {
if (user.length() == 0) {
addFieldError("user", getText("user required"));
}
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
I tried to eliminate form.jsp under \Web Pages by adding a new action method to MyAction.java.
@Action(value="bar", results = {
@Result(name = "success", location = "form.jsp"),
})
public String another() {
return "success";
}
But I got the following error when I go to http : //localhost …/bar.action
HTTP Status 404 – No result defined for action action.MyAction and result input
Your MyAction has an implementation of validate(), which means it is validation aware.
What’s happening is that you’re calling another, but validate() is kicking in (as it’s in the interceptor stack). Validation is failing, and therefore sending to INPUT result, which is not defined in another.
You should
On a more general note, when you get that kind of error (No result defined for action X and result input) it usually means you’re either having validation errors, parameter population errors (eg: an exception in preparable).