Car Rental Web and Annotations with Commons Validator

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.

In this article we will configure a Spring MVC project that uses Annotations with the popular Commons Validator Framework.

Please ensure you have done the following before continuing with this article.


After you have the car rental application functioning you can follow these simple steps to get your form validated with commons validator.

Ensure you have the following keys defined in the resource property file.

Insert the following into the resource bundle property file. If the values are not there then add them.

info.firstName=First Name
info.lastName=Last Name

My validation.xml file looks like this…

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"


 <form name="carReservation">
 <field property="party.firstName" depends="required">
 <arg key="info.firstName" />
 <field property="party.lastName" depends="required">
 <arg key="info.lastName" />


All that needs to be done is to specify the name of the form and the fields that need validation.

For each controller you need to inject the beanValidator so that it is available for the method that will handle the post.

There are 2 ways to get this done.

  1. Define your bean in a spring configuration file
  2. Use the @Autowired functionality

Option 1 kind of defeat’s the purpose of using Annotation based controllers. The whole point was not to have your controller defined in a spring configuration file.

Option 2 is better and that is what we will do here. Put the following into your Controller:

	private Validator beanValidator;	

We don’t need getters or setters for beanValidator, apparently spring takes care of setting the values internally.

Put the following into the begining of the method that handles the POST.

		beanValidator.validate(carReservation, result);
		if(result.hasErrors()) {
			return "carNew";

My NewRequestAnnotationController looks like this:

package test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

public class NewRequestAnnotationController {

	private Validator beanValidator;	

	@RequestMapping(method = RequestMethod.GET)
	public String processGET(ModelMap model) {
		CarReservation carReservation = new CarReservation();
		model.addAttribute("carReservation", carReservation);		
		return "carNew";
	@RequestMapping(method = RequestMethod.POST)
	public String processPost(
			@ModelAttribute("carReservation") CarReservation carReservation,
			BindingResult result, SessionStatus status) {
		beanValidator.validate(carReservation, result);
		if(result.hasErrors()) {
			return "carNew";
		System.out.println("processing post request.");
				+ carReservation.getParty().getFirstName());
				+ carReservation.getParty().getLastName());

		return "selectDate";

Save and restart…

You can navigate to your application by typing

That’s All!!!

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.

0 Responses to “Car Rental Web and Annotations with Commons Validator”

  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

August 2009
« Jul   Sep »

Blog Stats

  • 847,160 hits

%d bloggers like this: