This article explains how Valang Validation Framework is used in Spring Projects.
Introduction of Spring
Spring is essentially a technology dedicated to enabling you to build applications using POJOs. Spring make use of all open sources and make J2ee development easy.
Spring is portable between application servers like WebLogic, Tomcat, Resin, JBoss, Jetty, Geronimo, and WebSphere. Springs non-invasive, POJO, approach enables us to take advantage of environment-specific features without sacrificing portability.
Spring modules are grouped into:
i) Core Container,
ii) Data Access/Integration,
iii) Web,
iv) AOP (Aspect Oriented Programming),
v) Instrumentation and
vi) Test
Validation in Spring:
Spring answer for web development is spring_web_MVC. In spring web MVC, view pages are built using jsp, jstl etc, and this web pages can be validated using 3 ways:
- create a class that implements Spring's Validator interface and make validation rules programmatically
- use commons-Validator declarative validation, integrated into Spring in the springmodules projects (ie, not part of the core distribution)
- Valang, a validation language so that validations can be written declaratively in a ValangValidatorFactoryBean declaration in an xml file, and this will create an implementation of spring’s Validator. This is in the spring modules sandbox but has not yet been released.
Using Valang Validation we can do both client and service side validation in declarative way.
Valang Server Side Validation
Algorithm for Server side validation:
Step 1: Import valang specific latest jars into your web application.
Step 2: Create Command class (Form class).
Step 3: Configure this Command class (Form class) in Spring application context for example test-servlet.xml.
Step 4: Declare the Valang validation rules in separate xml file for example test-validations.xml or in same Spring application Context test-servlet.xml.
Step5: Configure the mapping of Valang validation declared in step4 to Controller.
Step6: Changes in view needs to be done for using validation rules defined in step4.
Step7: Configurations in web application context Web.xml.
Code Example for above algorithm:
Step1: Import Valang Jars like spring-modules-validation-0.7.jar, or which ever latest available.
Step2: Create Command class (Form class).
package com;
public class OrderBean {
String ordernumber;
String itemname;
/**
* @return Returns the itemname.
*/
public String getItemname() {
return itemname;
}
/**
* @param itemname The itemname to set.
*/
public void setItemname(String itemname) {
this.itemname = itemname;
}
/**
* @return Returns the ordernumber
*/
public String getOrdernumber() {
return ordernumber;
}
/**
* @param userName The ordernumber to set.
*/
public void setOrdernumber(String ordernumber) {
this.ordernumber = ordernumber;
}
}
Step3: Configuring Command Class in Spring application context test-servlet.xml.
<beans>
<bean id="orderbean" class="com.OrderBean"/>
</beans>
<bean id="orderController" class="com.OrderController">
<property name="sessionForm">
<value>true</value>
</property>
<property name="commandName">
<value> orderBean </value>
</property>
<property name="commandClass">
<value>com.OrderBean </value>
</property>
<property name="validator">
<ref bean="orderValidator"/>
</property>
</bean>
Step4: valang validation xml test-validations.xml
<bean id="orderValidator" class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<![CDATA[
{ itemname : ? is not blank :
'The report type is missing (valang).' : 'error.missing.itemname'}
{ ordernumber : ? is not blank :
'The report type is missing (valang).' : 'error.missing.ordernumber'} ]]>
</value>
</property>
</bean>
Step5: Configure the mapping of Valang validation declared in step4 to Controller.
Test-servlet.xml
<bean id="orderController" class="com.OrderController">
<property name="sessionForm">
<value>true</value>
</property>
<property name="commandName">
<value> orderBean </value>
</property>
<property name="commandClass">
<value>com.OrderBean </value>
</property>
<property name="validator">
<ref bean="orderValidator"/>
</property>
<property name="formView"><value>orderForm</value></property>
<property name="successView"><value>success</value></property>
</bean>
Step6: Changes in view for example order.jsp
<form:form method="post" commandName=" OrderBean ">
<tr> <td>
<font color="red">
<form:errors path="prefix" cssClass="error"/></font>
</td></tr>
<tr><th align="right">
<span class="required">*</span> Item Name:</th>
<td><form:input path="itemname" size="30"/></td>
</tr>
<tr><td><font color="red">
<form:errors path="itemname" cssClass="error"/></font>
</td></tr>
<tr><th align="right"><span class="required">*</span> Order Number:</th>
<td><form:input path="ordernumber" size="30"/></td>
</tr>
<tr><td>
<font color="red"><form:errors path="ordernumber" cssClass="error"/>
</font>
</td> /tr>
</form:form>
Step7: Configurations in web application context Web.xml.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/test-servlet.xml
/WEB-INF/test-validations.xml
</param-value>
</context-param>
Valang Client Side Validations
Algorithm for Client side validation:
All steps mentioned for server side validation are applicable to client side validation, plus few more changes in view files.
Example: Changes to be made in view file, for example in order.jsp are:
Step 1: import tag
<%@ taglib uri="http://www.springmodules.org/tags/valang" prefix="vl"%>
Step2: Include in html header tag.
<script type="text/javascript" src="<c:url value='/js/valang_codebase.js'/>"></script>
Step3: Insert below tag, just below html form tag.
<valang:validate commandName=" orderBean" />
Valang Custom Validations:
Validation those are not possible to configure using Valang validation rules. Are done using custom validator.
Algorithm: Valang custom validator.
Step1:Implementing customer validator as below :
package springapp.web;
import org.springframework.validation.Errors;
import org.springmodules.validation.valang.ValangValidator;
import springapp.web.Contact;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class FormBeanValidator extends ValangValidator {
public void validate(Object object, Errors errors) {
Contact form = (Contact) object;
if(isCorrectDOB(form.dateofbirth)) {
errors.rejectValue("dateofbirth", "error.dateofbirth","Date of birth can not
be empty!.");
}
//dont forget call to super so as the validations
//defined in XML take place
super.validate(object, errors);
}
private boolean isCorrectDOB(String DOB) {
//check if time is in future
if (DOB!= null || DOB !=""){
return true;
}
else {
return false;
}
}
}
Step2: Configure the custom validator FormBeanValidator .java created in step1 in test-validations.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id=" formBeanValidator " class="springapp.web. FormBeanValidator ">
<property name="valang">
<value>
<![CDATA[
{ prefix : ? is not blank : 'prefix: required.' }
{ firstName : ? is not blank : 'The report type is missing (valang).' : 'error.missing.firstName'}
{ lastName : ? is not blank : 'The report type is missing (valang).' : 'error.missing.lastName'}
{ address : ? is not blank : 'The report type is missing (valang).' : 'error.missing.address'}
</value>
</property>
</bean>
</beans>
</xml>
Step3: Configure test-validations.xml into web application context Web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app id="WebApp">
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/springapp-servlet.xml
/WEB-INF/ test-validations.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
</web-app>
Advantage & Disadvantages of using Valang Validation Framework.
Advantage:
- Valang validation is easily configurable in xml file.
- Same validations mentioned in XML can be used for both client and server side validation.
- Valang language script rules are easy to understand.
- Valang has many built in script for validation, most common validations for form elements are date, zip code, Email etc.
- Even it not so matured validation still we can use, for most common form validation on the fly.
Disadvantages:
- Its built in validations are available for only important form elements.
- If we can not do any validation using valang language, we need to go for custom validation for this. We needs to write both separate server validation (in Custom java class) and client side validation (in valang jar java script).
- Spring Team is still in processes of developing the valang integration with spring, this gets unnecessary problems if you are using older valang jars.
- Very less documentation is available with very few examples, and valang frame is under development, not very matured validation.
0 comments:
Post a Comment