Struts 2 @Action Annotation Example
May 13, 2014
@Action annotation in struts 2 defines URL pattern for an Action class. Each method of an action class can be fetched by a separate URL. This facility provides the power to hide package name in URL. It is also possible that more than one URL pattern can point a single point of execution. In this page we will see different cases how to use @Action annotation in struts 2.
Case 1: @Action at Method Level
@Result(name="success",location="/user/user.jsp") public class UserAction extends ActionSupport{ private static final long serialVersionUID = 1L; @Action("/url/one") public String one() { return SUCCESS; } }
http://localhost:8080/Struts2Demo-1/url/one
Case 2 : @Action at Class Level
@Namespace("/user") @Action("/login") @ResultPath(value="/") @Result(name="success",location="login.jsp") public class LoginAction extends ActionSupport{ public String execute() { return SUCCESS; } }
http://localhost:8080/Struts2Demo-1/user/login
Case 3 : @Actions with more than one @Action
@Result(name="success",location="/user/user.jsp") public class UserAction extends ActionSupport{ private static final long serialVersionUID = 1L; @Actions({ @Action("/abc/url"), @Action("/xyz/url") }) public String three() { return SUCCESS; } }
http://localhost:8080/Struts2Demo-1/abc/url
http://localhost:8080/Struts2Demo-1/xyz/url