Richard’s Weblog

January 23, 2009

Spring-MVC from scratch, the AbstractController.

Filed under: Spring Framework,Web development — Richard @ 9:39 pm
Tags: ,

In my last post on Spring MVC, I showed a brief example of how I could handle a form submission using almost no Spring configuration, and only the Controller interface.  Now I’m gonna take the same example, but using the AbstractController class.
Before jumping into the code, let’s talk about what AbstractController is, and how it works when a client request reaches it.

The AbstractController is an implementation of the Template Method design pattern.  In the legendary book titled “Design Patterns, Elements of reusable Object-Oriented software” the authors summerize this pattern with this description :

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.  Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

That’s what the AbstractController does : it serves as a skeleton for the algorithm to execute when a request comes in, perfoming some operations on the request and response objects, and then handing the rest of the processing to subclasses. 
The AbstractController enables a filtering of the HTTPmethods that can be used with the controller. For instance, if you need to block “POST” requests for a particular controller, you can do it at configuration time, using the spring IOC container :

<bean name="/book/search" class="com.company.x.samples.bookbay.search">
<property name="supportedMethods" value="get"/>

AbstractController can also control whether or not it will work without an HttpSession already bound to the request. Finally, it can be used to define HTTP caching behavior.
In my last post, I made an example of a controller handling a form, only using the Controller interface.  If I want to add the features offered by AbstractController described above, all I would have to do is make my class extend AbstractController instead of directly implementing Controller, and change my handleRequest method name to call it handleRequestInternal :

package com.company.x.samples.bookbay.search;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class SearchController extends AbstractController {

	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("/book/searchForm.jsp");

		if("POST".equalsIgnoreCase(request.getMethod())) {
			handleFormSubmission(request, mav);
		}
		return mav;
	}

	private void handleFormSubmission(HttpServletRequest request,
			ModelAndView mav) {
		String searchString = request.getParameter("searchString");
		mav.addObject("searchString", searchString);

		// if an empty search string have been entered, no need to go further
		if(searchString == null || searchString.trim().length() == 0)
			return;

		List<Book> books = new LinkedList<Book>();
		// Fake we found some results..
		Book book = new Book();
		book.setTitle("Test Driven Development");
		book.setIsbn("0-321-14653-0");
		book.setAuthor("Kent Beck");
		books.add(book);
		mav.addObject("books", books);
	}
}

Advertisements

1 Comment »

  1. Thanks for the useful and easy implemented examples

    Comment by aftab — January 18, 2011 @ 12:00 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

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

Create a free website or blog at WordPress.com.

%d bloggers like this: