JavaData & JavaExploring the Spring Web MVC for Web Application Development

Exploring the Spring Web MVC for Web Application Development

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The Spring framework is an eco-system of discrete modules, libraries, and frameworks that provide diverse utility leveraging enterprise application development, Web services, data persistence, OSGi to the .NET framework, and many more. Web MVC is a significant module from the Spring framework foundry that primarily targets in leveraging servlet and portlet environment of a Web application. Though it works in the view layer of the J2EE spectrum, within the view layer it expands itself in a MVC pattern. The article explores some of its key aspects that help to build loosely coupled Web applications on the Spring IoC container.

Architectural Pattern

Like most MVC framework Spring’s web MVC is request driven, thriving upon a central servlet called DispatchServlet. DispatchServlet is an implementation of Front Controller pattern of Java EE web tier. Front controller consolidates the request handling mechanism where all requests are channeled through single handler object. Entire controlling and administration of request handing mechanism is processed through DispatchServlet. The Front Controller pattern can be illustrated as follows (see Figure 1).

Spring1
Figure 1: Working procedure of the Front Controller pattern

As we can see, there are two distinct parts; one is the Web handler and another, the command hierarchy. The main responsibility of the Web handler is to process POST and GET requests from the Web server. The exact command to run is decided by the Web handler. This decision process is accomplished either statically or dynamically. In the static version, the URL is parsed and conditional logic is used to instantiate a command class. In the dynamic version, on the other hand, command objects are created at runtime from the URL. The main advantage of the static case is its simplicity of implementation whereas, in the dynamic case, new commands can be added without changing the Web handler. Though this is almost a rudimentary overview, it gives the necessary clue to understand Spring web MVC workflow. Thus, in view of the Front Controller pattern, the request, response Web flow of Spring web MVC can be illustrated as follows (see Figure 2).

Spring2
Figure 2: Request processing workflow of DispatchServlet in a Spring web MVC

  • The lient sends a HTTP request.
  • The DispatcherServlet intercepts the request and filters out the appropriate Handler Mapper.
  • Once the appropriate handler mapper is figured out, DispatcherServlet delegates the request to the appropriate Controller.
  • The Controller, on receiving the request, returns a model and view object in the form of a ModelAndView instance.
  • Now it is theDispatcherServlet‘s turn again to resolve the appropriate view with the help of a ViewResolver instance. The view may be such as JSP, FreeMarker, and so forth.
  • Lastly, the selected view is then delegated back to the client for rendering in the browser.

The Application Context

To map the servlet and to bootstrap the Web container DispatcherServlet must be configured. This is necessary; otherwise, it would not be possible to load the servlet and map it to one or more URL patterns. WebApplicationContext is the root context for the Web application. It is loaded before DispatcherServlet, to ensure that the required services and resources are available before running the application. WebApplicationContext configures itself after bootstrapping so that the servlet can detect the necessary components from the application context and figure out what to do (use defaults if specific configuration not found) on any missing components. There are different ways to configure and register a servlet. We may use any one of the methods stated below.

1. Using web.xml

<web-app version="3.0" xmlns=
   "http://java.sun.com/xml/ns/javaee"
      xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi_schemaLocation=http://java.sun.com/xml/ns/javaee http: /
      java.sun.com / xml / ns / javaee /
      web-app_3_0.xsd metadata-complete="true">
   <servlet>
      <servlet-name>mylibrary</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>mylibrary</servlet-name>
      <url-pattern>/*</url-pattern>
      </servlet-mapping>
</web-app>

2. Using web-fragment.xml

<web-fragment version="3.0"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi_schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
   <servlet>
      <servlet-name>bookstore</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>bookstore</servlet-name>
      <url-pattern>/*</url-pattern>
   </servlet-mapping>
</web-fragment>

3. Using javax.servlet.ServletContainerInitializer

import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.servlet.DispatcherServlet

public class MylibraryServletContainerInitializer
      implements ServletContainerInitializer {


   @Override
   public void onStartup(Set<Class<?>> classes,
   ServletContext servletContext) throws ServletException {
      ServletRegistration.Dynamic registration;
      registration = servletContext.addServlet("dispatcher",
         DispatcherServlet.class);
      registration.setLoadOnStartup(1);
      registration.addMapping("/*");
   }
}

4. Implementing the org.springframework.web.WebApplicationInitializer Interface

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;

public class MylibraryWebApplicationInitializer implements
      WebApplicationInitializer {

   public void onStartup(ServletContext servletContext)
         throws ServletException {
      ServletRegistration.Dynamic registration;
      registration = servletContext.addServlet("dispatcher",
         DispatcherServlet.class);
      registration.addMapping("/*");
      registration.setLoadOnStartup(1);
   }
}

Spring MVC Components Detection

There are numerous components expressed as interfaces. These components are resolved by the DispatcherServlet from the instance of the application context, WebApplicationContext. Once configured, the initialization request is received from the container; if a configured component is not found, the default implementation of the component is used. The problem is resolved in the following manner (see Figure 3).

Spring3
Figure 3: Component detection by DispatcherServlet

Here is a list of commonly used components and their default implementations.

Component Default Implementation
HandlerMapping BeanNameUrlHandlerMapping, DefaultAnnotationHandlerMapping
HandlerAdapter HttpRequestHandlerAdapter, SimpleControllerHandlerAdapter, AnnotationMethodHandlerAdapter
HandlerExceptionResolver AnnotationMethodHandlerExceptionResolver, ResponseStatusExceptionResolver, DefaultHandlerExceptionResolver
ViewResolver InternalResourceViewResolver
FlashMapResolver SessionFlashMapManager
RequestToViewNameTranslator DefaultRequestToViewNameTranslator
ThemeResolver FixedThemeResolver
LocaleResolver AcceptHeaderLocaleResolver
MultipartResolver There is no default for this; must be configured explicitly.

Conclusion

These are some of the tidbits of Spring Web MVC. The Spring framework evolved from the view of Rod Johnson. The idea is to look for a solution nonconforming EJB in the enterprise arena. Practically, EJB was horrible until JEE6 (IMHO) and prior to that. A Spring framework is thus a nonconforming revolutionary that pioneered a different path. However, things have rapidly changed until JEE7 and working with EJB is as simple as a POJO. Spring’s versatility never fell short to impress developers around the world. Now, both these stalwarts, Spring and JEE, learned and borrowed from each other, and both stand tall with equal promise. So, if you want to collaborate or use a part of Spring, such as web MVC with JEE7 and vice versa, it’s easily alignable.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories