Car Rental Web with Annotations

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.

Annotations can be used with Spring MVC to process HTML form input. After the form input is processed the user is forwarded to a confirmation page. This article will focus on the Car Rental application we developed earlier in this series. We will take that application and convert it to use Annotation based configuration.

If you have not done so already please make sure you satisfied the following requirements.


The car rental web application is a standard “classic” Spring MVC project that takes input from the user and processes it. Then it forwards to the next page. In later versions of that application commons validation and valang were added as part of the processing but that is beyond the scope of what we want to cover here.

After configuring and testing the Car Rental Web Application you can add support for annotations by adding the following 3 lines to your spring-servlet.xml file.

<context:component-scan base-package="test"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

The first line accepts a comma separated list of packages that need to be searched for annotations. Since all the files are located inside the “test” package we can leave this line as-is.

To use annotation and non-annotation controllers together just specify the BeanNameUrlHandlerMapping and an order attribute.

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="0" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

Since we will be mixing pre 2.5 Spring MVC with the latest Annotation based controllers we need to specify the following line as well. The reason for this is that the Spring Dispatcher servlet pre-configures its self with sensible defaults. The handlerAdapter is one of the defaults it has. If you specify another handler adapter implementation it will blindly use it. For this purpose you need to restore the default handler adapter below.

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

The NewRequestFormController is a SimpleFormController. These type of controllers are specifically designed for Form Processing. Form interaction is a 2 step process. The first step is to display a blank, or pre-filled form to the user. The user fills out the form and submits it back to the application. The application takes the form data validates and processes it and forwards the user on to the success page.

Lets prepare our annotated class to accept user data.

In our class we will put the following annotation to handle requests that come in as GET requests.

@RequestMapping(method = RequestMethod.GET)

package test;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class NewRequestAnnotationController {

	@RequestMapping(method = RequestMethod.GET)
	public String processGET(ModelMap model) {
		CarReservation carReservation = new CarReservation();
		carReservation.getAddress().setName("Frank Default");
		model.addAttribute("carReservation", carReservation);
		return "carNew";


In the above example we specify that the processGET() method will be called when the page is first requested. In the first request we will initialize the carReservation command bean and associate it with the request. This is a perfect time to pre-fill the form before the user starts inputting values.

In the next step the user will be presented with the form. They will fill it out and hit submit. At this time the control will return to this bean. It’s method will be a POST since we specified it as such in the form.

Add the following to the NewRequestAnnotationController

	@RequestMapping(method = RequestMethod.POST)
	public String processPost(
			@ModelAttribute("carReservation") CarReservation carReservation,
			BindingResult result, SessionStatus status) {

		System.out.println("processing post request.");
				+ carReservation.getParty().getFirstName());
				+ carReservation.getParty().getLastName());

		return "selectDate";

In this example we have specified the form in the @ModelAttribute annotation in the method parameter list.

The methods that are responsible for processing the post request are flexable. There are a few variations you can specify. In order to get a more detailed understanding please see my related article. HelloWorld Spring MVC with Annotations

You can run this example by navigating to the following URL:

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 with Annotations”

  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

September 2009
« Aug   Oct »

Blog Stats

  • 846,580 hits

%d bloggers like this: