Spring MVC Validations Example

springMost 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.

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.

helloworld.jsp

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.

 Output

  • Share/Bookmark
Spring, Tech Notes

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

2 Responses to “Spring MVC Validations Example”

Leave Comment

(required)

(required)