Spring MVC Validations Example
Most of the presentation tier frameworks provide support to client side validations. Spring MVC is not different in this principle. Just that the way it is implemented is little different. You must have gone through the painful Javascript based client side validation already, and must be in the search of a better way of doing the validations. Then Spring MVC’s solution is the right and a quick way. The best thing about this validation framework is that, it is easy to learn as well as implement. I mean you don’t need to do too many things to get it working. These are the steps to get the validation working.
- Write a validator class which implements Spring’s Validator interface, and also the validations in Java (optionally using Spring’s validation utility class)
- Attach it to the controller and view through configuration.
- Change Jsps to display the error messages.
We take our previous article example and go through these changes.
Additional Jars:
Spring Validator interface is a part of Spring’s general purpose jar and not of Spring-mvc.jar. Hence we need to add this to the classpath.
Spring.jar
Java Changes:
The validator class is an addition. This checks if the first name is null or blank. You can use Spring’s ValidatorUtils or even external utilities like org.apache.commons.validator.GenericValidator to do same thing instead of coding on your own. Finally, you have to add an error message to the errors object. How you validate to arrive at the error message is up to you.
VisitorValidator.java
package com.myorg.springmvctutorial.web.validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.myorg.springmvctutorial.web.command.Visitor;
public class VisitorValidator implements Validator{
public boolean supports(Class aClass) {
return Visitor.class.equals(aClass);
}
public void validate(Object arg0, Errors errors) {
Visitor visitor = (Visitor)arg0;
if(visitor.getFirstName()==null || visitor.getFirstName().equals("")){
errors.rejectValue("firstName", "firstName.empty", "Required Field.");
}
}
}
Configuration Changes:
There are two changes to be done to the SpringMVCTutorial-servlet.xml. First one is addition of validator and formview (the view which populates the command object to be validated) to the HelloVisitorController definition, and second is addition of message resource bundle. (This is optional, but if you look at large applications, it is not good idea to leave the error messages in code. Also, it is good for multilingual support.)
SpringMVCTutorial-servlet.xml
helloWorldController helloVisitorController visitor helloworld org.springframework.web.servlet.view.tiles2.TilesView
/WEB-INF/layout-tiles.xml messages
Jsp Changes:
The error message thrown by the validator class needs to be displayed on jsp. We use the jstl core tag to do this. Next is change around the firstName field to display the error message.

Message Properties:
This is optional change but good to have in large applications. Add this messages.properties to /WEB-INF/classes directory.
/WEB-INF/classes/messages.properties
firstName.empty=First Name can not be empty.
See Everything Working:
Deploy the application on server and call the url of helloworld.htm. Do not enter anything and press the submit button to see following screen. Next is enter some value in first name and see it not showing any error.










Here, I can’t see the changes which u have made in this jsp file. Pls tell me any other option to see this jsp code.
Hi Vasu,
Here is code for the jsp.
<%@ taglib uri=”http://www.springframework.org/tags” prefix=”spring”%>
<%@ taglib prefix=”form” uri=”http://www.springframework.org/tags/form”%>
<h1>This is Spring MVC Tutorial</h1>
<spring:nestedPath path=”visitor”>
<form:form action=”hellovisitor.htm” commandName=”visitor”>
<TR>
<TD class=”alignRight” nowrap width=”156″>First Name: </TD>
<TD class=”alignLeft” colspan=”3″>
<spring:bind path=”firstName”>
<input name=”${status.expression}”
value=”${status.value}” type=”text” id=”textfield” accesskey=”" tabindex=”1″>
</spring:bind>
</TD>
</TR>
<TR>
<TD class=”alignRight” nowrap width=”156″>Last Name: </TD>
<TD class=”alignLeft” colspan=”2″>
<spring:bind path=”lastName”>
<input name=”${status.expression}” value=”${status.value}”
type=”text” id=”textfield1″ accesskey=”" tabindex=”2″>
</spring:bind>
</TD>
</TR>
<TR>
<TD>
<input type=”submit” value=”Submit”>
</TD>
</TR>
</form:form>
</spring:nestedPath>
Leave your response!
Subscribe
Subscribe Via Email
Recent Posts
Recent Comments
Tags
Categories
Archives