Spring MVC validation Configuration
Commons Validation
Pros:
Optimal use of use of resources: JavaScript validations are provided when JavaScript is enabled, and server-side validations are guaranteed.
A single point of maintenance: both client-side and server-side validations are generated from the same configuration and use the same error message.
Cons:
Lack of data conversions and transformations
The generated JavaScript is no modal, it does not engage until the form is submitted.
Spring Validation
Pros:
Server-side validation is easy to implementorg.springframework.validation.Validator.
Use the same error message as commons validation. Easy internationalization.
Cons:
Spring client-side validation is in development progress, need standard version.
Configuration:
1. WEB-INF/validation-rules.xml
default validation rulessuch as email , creditcard, date validation rules.
2. WEB-INF/validation.xml
User-defined form validation rules.
<form-validation>
<formset>
<form name="account">
<field property="username" depends="required">
<arg0 key="signonusername"/>
</field>
<field property="password" depends="required">
<arg0 key="signonpassword"/>
</field>
<field property="lastName" depends="required">
<arg0 key="accountfirstname"/>
</field>
<field property="firstName" depends="required">
<arg0 key="accountlastname"/>
</field>
<field property="email" depends="required, email">
<arg0 key="accountemail"/>
</field>
</form>
</formset>
</form-validation>
3. Web-INF/iadvisezou-servlet.xml add bean configuration
<beanid="validatorFactory" class="org.springmodules.commons.validator.DefaultValidatorFactory">
<property name="validationConfigLocations">
<list>
<value>/WEB-INF/validator-rules.xml</value>
<value>/WEB-INF/validation.xml</value>
</list>
</property>
</bean>
<!-- Validating the Spring way -->
<bean id="userValidator" class="iadvisezou.web.UserValidator"/>
<!--Commons Validation define -->
<bean id="beanValidator" class="org.springmodules.commons.validator.DefaultBeanValidator">
<property name="validatorFactory" ref="validatorFactory"/>
</bean>
4.Web-INF/iadvisezou-servlet.xml Add validator property in Spring contoller
<bean id="createUserController" class="iadvisezou.view.controller.CreateUserController">
<property name="commandName" value="account"/>
<property name="formView" value="createUser"/>
<property name="successView" value="redirect:logout.jsp"/>
<property name="serviceLocator" ref="serviceLocator"/>
<property name="validators">
<list>
<ref bean="beanValidator"/> <!--Commons Server-side validation -->
<ref bean="userValidator"/> <!--Spring validation -->
</list>
</property>
5. WEB-INF/classes/message_en_US.properties(English version)Add error message for the validation.
# -- validator errors --
errors.required= {0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
6.Add class Iadvisezou.view.validator.UserValidator Spring server-side validator class.
7. Modify taglibs.jsp add jsp file tag definition
<%@ tglib uri=" prefix="validator" %>
8. Modify /jsp/createUser.jsp add commons validation tag
Modify:
<form method="post" action="<c:url value="/createUser.html"/>" onsubmit="return validateAccount(this)">
Add in the last line:
<validator:javascript formName="account"
staticJavascript="false" xhtml="true" cdata="false"/>
<script type="text/javascript"
src="<c:url value="/scripts/validator.jsp"/>"</script>
9: Add/scripts/validator.jsp
<%@ page language="java" contentType="javascript/x-javascript" %>
<%@ taglib uri=" prefix="validator" %>
<validator:javascript dynamicJavascript="false" staticJavascript="true"/>