Implementation of Subframework validation Struts2.0 Framework

Implementation of Subframework validation Struts2.0 Framework

Previous Home Next

 

In order to applied validator on the field an action to validation mapping file need to be created. It is an XML file that maps action fields and predefined validators. For each action a different validation mapping is created. Conventionally a validation mapping file name as follows:

ActionClassName-validation.xml

A validation mapping file has following elements:-

Validators:-The validators is the root elements of validation mapping file.

Field:-Fields elements is the subelements of validators that is used to defined validation mapping for fields. It has following subelements.

<field-validator>:- The field-validator is used to specified validator to be used for validating the field. It contains following elements
<param>
<message>

 
Directory Structure of Subframework Validation Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<s:form action="register">
<s:textfield name="name" label="Name"/>
<s:password name="password" label="Password"/>
<s:textfield name="age" label="Age"/>
<s:textfield name="mailId" label="MailId"/>
<s:submit value="Register"/>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

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="demo" extends="struts-default">
<action name="register" class="mypack.MyAction">
<result name="success">/hello.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>

MyAction.java

package mypack;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport{
	private String name,password,mailId;
	private int age;
	@Override
	public void validate() {
		System.out.println("Validate method is invoked");
		super.validate();
	}
	public String execute(){
		System.out.println("Validate method is not invoked");
		return "success";
		}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMailId() {
		return mailId;
	}
	public void setMailId(String mailId) {
		this.mailId = mailId;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

MyAction-validation.xml

<!DOCTYPE validators PUBLIC 
"-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
  		
<validators>
<field name="name" >
<field-validator type="requiredstring">
<message>Name must be required.</message>
</field-validator>
<field-validator type="regex">
<param name="expression">[A-Z,a-z, ]*</param>
<message>Name must in alphabet. and space.</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Password must be required.</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">10</param>
<message>Password must be b/w 5 and 10.</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">18</param>
<param name="max">45</param>
<message>Age must be in 18 to 45.</message>
</field-validator>
</field>
<field name="mailId">
<field-validator type="requiredstring">
<message>MailId must be required.</message>
</field-validator>
<field-validator type="email">
<message>MailId isn't in proper format.</message>
</field-validator>
</field>
</validators> 

hello.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>

<b>Welcome,<s:property value="name"/>

Output



Previous Home Next