Validation with Bean Validation Framework

May 3 2007one Commented

Categorized Under: Java, Object Oriented and Design

Spring framework provide the nice approach of validation that separate with the main business with validation. There are 3 ways of validation supported by Spring framework, they are: Common Validator, Valang (Va-lidation Lang-uage) Validator and Bean Framework Validator. According to me, Bean Framework Validator is the best among these approaches because it is more flexible, more powerful (support more types of validation Domain Model Constraints, Business Rules and Application Constraints besides Data Constraints). Bean Validation Framework provide the way to enhance its capacity, however you rarely need to enhance because it provides enough data constraint validation in almost cases. Compare with Hibernate Annotation, Bean Framework Validation also gain my vote per its advantages. One more reason that I choose Bean Validation Framework in my project that it supports XML declaration instead using annotations (though it support annotation too) because my beans are generated automatically and the Netbeans can not know to data constraint base on database schema.

Using Bean Validation Framework is rather easy, you declare the validation in spring context file and that is. Here is the sample of my context file:

<bean id="registerUserValidator" class="org.springmodules.validation.bean.BeanValidator">
<property name="configurationLoader" ref="configurationLoader"/>
</bean>
<bean id="configurationLoader" class="org.springmodules.validation.bean.conf.loader.xml.DefaultXmlBeanValidationConfigurationLoader">
<property name="resource" value="classpath:beanValidation/userDTOValidation.xml"/>
</bean>
<bean id="registerForm"
class="ows.security.controller.RegisterUserController">
<property name="sessionForm" value="true" />
<property name="commandName" value="userDTO" />
<property name="commandClass"
value="ows.security.admin.domain.UserDTO" />
<property name="validator" ref="registerUserValidator"/> <!--Register the validator to Controller-->
<property name="formView" value="admin/registration" />
<property name="successView" value="../index.jsp" />
<property name="userService" ref="userService"/>
</bean>

Note that the bean name configurationLoader has resource property that makes you define the validation file for bean, in my case I put it in the folder beanValidation under classpath property. Here is the content of userDTOValidation.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<validation xmlns="http://www.springmodules.org/validation/bean"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springmodules.org/validation/bean
http://www.springmodules.org/validation/bean/validation.xsd">
<class name="ows.security.admin.domain.UserDTO">
<validator class="ows.security.validator.UserValidator" /> <!--I can check the application and business constraints in this class while rely data constraint validation for spring framework->
<global>
<!-- global validation rules go here -->
</global>

<property name="firstname">
<not-blank message="First name can not be blank"/>
<length min="1" max="20"/>
</property>
<property name="lastname">
<not-blank message="Last name can not be blank"/>
<length min="1" max="20"/>
</property>
<property name="email">
<email message="Email is invalid"/>
</property>
<property name="password">
<length min="6" max="20" message="Password length must be in range [6..20]"/>
</property>
<property name="username">
<length min="3" max="20" message="User name length must be in range [3..20]"/>
</property>
</class>
</validation>

Remember you installed the spring modules before, and it is the only one reminder to use this nice framework :-)

Subscribe

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • blinkbits
  • BlinkList
  • blogmarks
  • BlogMemes
  • BlogMemes Cn
  • BlogMemes Fr
  • BlogMemes Jp
  • BlogMemes Sp
  • Blogosphere News
  • Blogsvine
  • blogtercimlap
  • Book.mark.hu
  • Bumpzee
  • co.mments
  • connotea
  • De.lirio.us
  • Design Float
  • DotNetKicks
  • DZone
  • eKudos
  • email
  • Fark
  • Faves
  • feedmelinks
  • Fleck
  • Furl
  • GeenRedactie
  • Global Grind
  • Gwar
  • Haohao
  • HealthRanker
  • Hemidemi
  • Identi.ca
  • IndianPad
  • Internetmedia
  • kick.ie
  • Kirtsy
  • laaik.it
  • Leonaut
  • LinkaGoGo
  • LinkArena
  • LinkedIn
  • Linkter
  • Live
  • Ma.gnolia
  • Meneame
  • MisterWong
  • MisterWong.DE
  • muti
  • MyShare
  • MySpace
  • N4G
  • Netvibes
  • Netvouz
  • NewsVine
  • NuJIJ
  • Ping.fm
  • PlugIM
  • Pownce
  • ppnow
  • Print
  • Propeller
  • Ratimarks
  • Rec6
  • Reddit
  • SalesMarks
  • Scoopeo
  • scuttle
  • Segnalo
  • Shadows
  • Simpy
  • Slashdot
  • Smarking
  • Socialogs
  • SphereIt
  • Spurl
  • StumbleUpon
  • Symbaloo
  • Taggly
  • TailRank
  • Technorati
  • ThisNext
  • Tipd
  • Tumblr
  • TwitThis
  • Upnews
  • Webnews.de
  • Webride
  • Wikio
  • Wikio FR
  • Wikio IT
  • Wists
  • Wykop
  • Xerpi
  • Yahoo! Buzz
  • YahooMyWeb
  • Yigg

One Response to “Validation with Bean Validation Framework”

  1. I appreciate your site very much. Will bookmark. Keep up to excellent info on it. ty

Leave a Reply