I'm following the Struts 2 Hello World Annotation Example tutorial by Mkyong:
@Namespace("/User")
@ResultPath(value="/")
@Action(value="/welcome",
results={@Result(name="success", location="pages/welcome_user.jsp")})
public class WelcomeUserAction extends ActionSupport {
public String execute(){
return SUCCESS;
}
}
Accessing the URL http://localhost:8080/project_name/User/welcome
works fine.
Now I'm trying to move the @Action (and hence @Result) annotation from class level to method level:
@Namespace("/User")
@ResultPath(value="/")
public class WelcomeUserAction extends ActionSupport {
@Action(value="/welcome",
results={@Result(name="success", location="pages/welcome_user.jsp")})
public String execute(){
return SUCCESS;
}
}
But after doing this, I get the 404 error:
/project_name/pages/welcome_user.jsp
is not found.
My JSPs are under
/WebContent/User/pages
Why is this happening ?
Since Struts2 will look for your JSPs in
WebContent/@ResultPath/@Namespace/@Result
Instead of doing
@ResultPath("/")/@Namespace("/User")/@Result("pages/welcome_user.jsp")
you could move your JSPs from
WebContent/User/pages/welcome_user.jsp
to
WebContent/pages/User/welcome_user.jsp
and then using
@ResultPath("/pages")/@Namespace("/User")/@Result("welcome_user.jsp")
At this point, both the following configurations should work:
With @Action
at class level:
@ResultPath(value="/pages")
@Namespace("/User")
@Action(value="/welcome", results={@Result(name="success", location="welcome_user.jsp")})
public class WelcomeUserAction extends ActionSupport {
public String execute(){
return SUCCESS;
}
}
With @Action
at method level:
@ResultPath(value="/pages")
@Namespace("/User")
public class WelcomeUserAction extends ActionSupport {
@Action(value="/welcome", results={@Result(name="success", location="welcome_user.jsp")})
public String execute(){
return SUCCESS;
}
}
I don't know why Mkyong's example works only with annotations at class level, and I'm waiting for someone more expert to fullfil our curiosity; meanwhile, this should be what you need.