Struts 2 XML Validation Example
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()
package net.javatips;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private Integer age;
private String password;
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;
}
}
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private Integer age;
private String password;
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;
}
}
RegisterAction-validation.xml
Our Action class name is RegisterAction, so struts2 validation.xml name must be RegisterAction-validation.xml (ie; append -validation with action class)
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="errors.required" /> </field-validator> </field> <field name="age"> <field-validator type="required"> <message key="errors.required" /> </field-validator> <field-validator type="int"> <param name="min">1</param> <param name="max">100</param> <message key="errors.range" /> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="errors.required" /> </field-validator> </field> </validators>
RegisterAction.properties
username=UserName age=Age password=Password errors.invalid=${getText(fieldName)} is invalid. errors.required=${getText(fieldName)} is required. errors.number=${getText(fieldName)} must be a number. errors.range=${getText(fieldName)} is not in the range ${min} and ${max}.
struts.xml
Following is the struts 2 configuration, which will used for naviagating different page views
The name of this configuration file should be "struts.xml"
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="register" class="net.javatips.RegisterAction"> <result name="success">RegisterSuccess.jsp</result> <result name="input">Register.jsp</result> </action> </package> </struts>
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>