Struts 2 Annotations: Struts 2 application can be developed with xml configuration and with annotations also.
For annotation we need to import below mentioned jar in our project:
struts2-convention-plugin-x.jar
Example of action with annotations:
import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.validator.annotations.*; //Results using annotation @Results({ @Result(name="success", location="/success.jsp"), @Result(name="input", location="/index.jsp") }) public class Employee extends ActionSupport{ private String name; //Action Mapping using annotation @Action(value="/personname") public String execute() { return SUCCESS; } //Apply validation using annotation @RequiredFieldValidator( message = "Name is mandatory" ) public String getName() { return name; } public void setName(String name) { this.name = name; } }
Here in the above example if success is returned then success.jsp will be displayed and if name is not filled then input error and index.jsp will be displayed.