Introduction to Spring MVC 3

Spring MVC is a web framework based on the model view controller design pattern. It is comparable to frameworks like Struts, JSF and Webwork among others. What makes Spring MVC special is that you don’t need to create classes that extend or implement spring specific classes or interfaces, and you don’t need to implement methods with special signatures. In fact there is so much flexibility that it is almost overwhelming. Methods in your Controller classes, configured with annotations, can take any number and just about any type of arguments and return just about anything. Here is a list of arguments and return types.

Here is how the request process lifecycle works in Spring MVC (as shown on the spring source page):

1. The client sends an http request to the web container.
2. The incoming request is received by the Front Controller (the DispatcherServlet) and it will then search for an appropriate Handler Mapping which will determine how requests are handled. If no Handler Mapping is configured then it will return the default mapping, the BeanNameUrlHandlerMapping.
3. The Front Controller (DispatcherServlet) will interpret the handler mappings, as configured via xml or annotations, and dispatch the request to the appropriate Application Controller (that’s the java Controller that you write).
4. The Application Controller processes the request, often creating a model and populating its fields, and returns an object (a model, or a view, or model and view…) to the Front Controller.
5. The Front Controller then resolves the View by finding and consulting the View Resolver object. One example of a view resolver is the InternalResourceViewResolver, which can be configured via xml.
6. The selected view is then rendered back to client.

Spring MVC has evolved rapidly over the last several years which leaves us with endless implementation choices. After version 2.5 we can use annotations which make life much easier in my opinion because it saves us from writing and maintaining thousands of lines of xml code. Xml configuration code is more error prone when it is lengthy and can cause the application to break during refactoring.

We are going to find the simplest possible way to implement Spring MVC 3 using annotations now. Let’s not let all the implementation possibilities cause us to make a mess of the code. Instead we must try to be consistent across our code so it will continue to be readable.

Return types

To process the client requests, we will create handler methods in our application controller called that will just return a String which will be used to resolve the resulting view back to the client. That’s right, just a basic java String! Nothing else. We will set up this system by defining an InternalResourceViewResolver to “resolve” the String to a jsp view for the end result. This resolver will take care of adding the right prefix and suffix so we don’t have to worry about hard coding paths into our java classes. A String is not the only type that can be returned; it is also possible to return a model object or a ModelAndView object. Here are all the possible return types.

Input parameter types

Our method parameters to these handler methods will be simple also. We will take method input parameters such as java.util.Map to represent the model object. The model does not have to be a spring specific object which is great! This will make testing much easier, as we will see later. We can take any number of parameters in our method however, so at a later point we will create handler methods that take multiple inputs: Map and OurCustomWebForm for example. It is also possible to have other input parameter types and here is a list of all the possibilities.

Handler Mappings

Best of all, we will see that mapping and handling requests is a breeze with annotations. Instead of configuring the mappings in xml, we will put the @RequestMapping annotation mappings right above the java methods that will handle those requests. It seems magic but it’s not. We will be using BeanNameUrlHandlerMapping as the Handler Mapping (as default) but there is actually no clear “bean” here which makes it kind of confusing. That’s because this is how one used to do things with the xml configuration “old school” way:

<bean name="/addgreeting.html" class="com.bitbybit.web.controller.GreetingController">
    <property> ..... </property>

With annotations, mapping is done differently:

@RequestMapping(value = "/addgreeting.html", method = RequestMethod.GET)
public String showAddGreetingPage(@ModelAttribute("greetingform") GreetingForm greetingForm)
       //resolves to /WEB-INF/jsp/addgreeting.jsp
       return "addgreeting";

The DispatcherServlet

To make this all happen, we must first configure a DispatcherServlet. Spring MVC revolves around the DispatcherServlet which is the main Front Controller for handing all incoming requests. We configure this servlet in web.xml and give it a name, among other things. We will also create a special xml file just for this servlet and its configuration xml code. Since we are using annotations, the configuration code will now reside in the java classes (the GreetingController class for example) instead of in the xml file.

Anonymous wrote on 2013-05-05 at 17:27:55:
Nice 1...keep it up.

Clement wrote on 2012-09-27 at 14:39:20:
This is exceptional. I appreciate the author doing a great work.

click wrote on 2012-08-12 at 19:09:03:
This blog site is very good! How was it made ?

Post a Comment