Struts 2 Validation Using Annotation

Struts2 Validation Using Annotation explains about step by step example of Struts2 Validation With Eclipse
Apache Struts 2, is a popular Java Model-View-Contraller (MVC) framework, which is developed by merging WebWork and Struts 1.x web frameworks.
Struts 2 have lot off difference with struts 1, struts 2 have implemented with an idea of convention over configuration, it also have many annotations in order to increase the developer productivity.
Struts 2 provides in built validation features. You can see how to validate the fields in struts 2, on below example
By using annotation in struts 2, you can remove struts.xml configuration
Required Libraries
Following jar must be in classpath
- asm-3.3.jar
- asm-commons-3.3.jar
- commons-fileupload-1.3.jar
- commons-io-2.0.1.jar
- commons-lang3-3.1.jar
- commons-logging-1.1.3.jar
- freemarker-2.3.19.jar
- javassist-3.11.0.GA.jar
- ognl-3.0.6.jar
- struts2-convention-plugin-2.3.15.1.jar
- struts2-core-2.3.15.1.jar
- xwork-core-2.3.15.1.jar
You can see the project structure below
Register.jsp
<%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <s:head /> </head> <body> <s:form action="register" method="post"> <s:textfield name="username" label="User Name" /> <s:textfield name="age" label="Age" /> <s:password name="password" label="Password" /> <s:submit value="register" /> </s:form> </body> </html>
RegisterSuccess.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <html> <body> <h1>Register Success</h1> </body> </html>
RegisterAction.java
Here is the action class, We are using Struts in built custom validate() method and throwing addFieldError()
You can see below Results tag, Here we have 2 results, if it is outcome is success it is forward to RegisterSuccess.jsp, otherwise it will show Register.jsp
@Results({
@Result(name="success", location="/RegisterSuccess.jsp"),
@Result(name="input", location="/Register.jsp")
})
Here @Results tag contains collection of results
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.ActionSupport;
@Results({
@Result(name="success", location="/RegisterSuccess.jsp"),
@Result(name="input", location="/Register.jsp")
})
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private Integer age;
private String password;
@Action(value = "/register")
public String execute() {
return "success";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void validate() {
if (getUsername().length() == 0) {
addFieldError("username", "User Name is required");
} else if (!getUsername().equals("Rockey")) {
addFieldError("username", "Invalid User");
}
if (getPassword().length() == 0) {
addFieldError("password", "Invalid password");
}
if (getAge() == null) {
addFieldError("age", "Age required");
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Run
You need to invoke http://localhost:8080/Struts2Validation/Register.jsp