Spring 3 MVC – Introduction to Spring 3 MVC Framework


Introduction to Spring 3 MVC Framework

Spring MVC is the web component of Spring’s framework. It provides a rich functionality for building robust Web Applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also Spring can integrate effortlessly with other popular Web Frameworks like Struts, WebWork, Java Server Faces and Tapestry. It means that you can even instruct Spring to use any one of the Web Frameworks. More than that Spring is not tightly coupled with Servlets or JSP to render the View to the Clients. Integration with other View technologies like Velocity, Freemarker, Excel or Pdf is also possible now.

In Spring Web MVC you can use any object as a command or form-backing object; you do not need to implement a framework-specific interface or base class. Spring’s data binding is highly flexible: for example, it treats type mismatches as validation errors that can be evaluated by the application, not as system errors. Thus you need not duplicate your business objects’ properties as simple, untyped strings in your form objects simply to handle invalid submissions, or to convert the Strings properly. Instead, it is often preferable to bind directly to your business objects.

Request Processing Lifecycle

spring-mvc-request-process-lifecycle
Image courtesy: Springsource

Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’s DispatcherServlet is completely integrated with Spring IoC container and allows us to use every other feature of Spring.

Following is the Request process lifecycle of Spring 3.0 MVC:

  1. The client sends a request to web container in the form of http request.
  2. This incoming request is intercepted by Front controller (DispatcherServlet) and it will then tries to find out appropriate Handler Mappings.
  3. With the help of Handler Mappings, the DispatcherServlet will dispatch the request to appropriate Controller.
  4. The Controller tries to process the request and returns the Model and View object in form of ModelAndView instance to the Front Controller.
  5. The Front Controller then tries to resolve the View (which can be JSP, Freemarker, Velocity etc) by consulting the View Resolver object.
  6. The selected view is then rendered back to client.

Features of Spring 3.0

  • Spring 3.0 framework supports Java 5. It provides annotation based configuration support. Java 5 features such as generics, annotations, varargs etc can be used in Spring.
  • A new expression language Spring Expression Language SpEL is being introduced. The Spring Expression Language can be used while defining the XML and Annotation based bean definition.
  • Spring 3.0 framework supports REST web services.
  • Data formatting can never be so easy. Spring 3.0 supports annotation based formatting. We can now use the @DateFimeFormat(iso=ISO.DATE) and @NumberFormat(style=Style.CURRENCY) annotations to convert the date and currency formats.
  • Spring 3.0 has started support to JPA 2.0.

Configuring Spring 3.0 MVC

The entry point of Spring 3.0 MVC is the DispatcherServlet. DispatcherServlet is a normal servlet class which implements HttpServlet base class. Thus we need to configure it in web.xml.

<web-app>

    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

In above code snippet, we have configure DispatcherServlet in web.xml. Note that we have mapped *.html url pattern with example DispatcherServlet. Thus any url with *.html pattern will call Spring MVC Front controller.
spring-3-mvc-architecture
Image courtesy: Springsource
Once the DispatcherServlet is initialized, it will looks for a file names [servlet-name]-servlet.xml in WEB-INF folder of web application. In above example, the framework will look for file called example-servlet.xml.

Note the above architecture diagram. The WebApplicationContext specified in above diagram is an extension of the plain ApplicationContext with some extra feature necessary for web applications. The WebApplicationContext is capable of resolving themes and it is also associated with corresponding servlet. The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext.

Moving On

Now that we have idea about architecture of Spring 3.0 MVC framework and its lifecycle, in the next part we will create a working Spring 3.0 Hello World application from scratch.



75 Comments

  • Anurag 30 June, 2013, 3:31

    Thanks… It’s informative and very methodical.

  • yose 13 July, 2013, 4:22

    hello!! Good!!

  • Abdul vasih T 11 September, 2013, 0:23

    Very simple and really informative..Thanks alot Viral… :)

  • Himanshu 19 September, 2013, 16:54

    Hey,
    I have done exactly as instructed, but i m getting HTTP Status 404 error.
    Server i used is jBoss6.0 . please help me out.

    • neeraj kumar 18 November, 2013, 17:09

      pls check connection. Controller and dispatcher-servlet or the directory..

  • ashok 6 November, 2013, 1:17

    thankx for the post it was very helpful

  • sunder 8 November, 2013, 11:06

    Hi,
    can you post any blogs which related to integration with spring hibernate and Dwr concept???

  • Naga 21 November, 2013, 14:52

    It is very simple and understandable.Thanks for giving good explanation.

  • gaurav 29 November, 2013, 13:46

    hey its not working while i do same to same and its give error when i nevigate page from welcome page to hello.jsp Error “page not found “

  • shrutika 1 May, 2014, 10:56

    Thanks this was really informative!!!

  • venkat 14 May, 2014, 11:45

    Thanks its very Informative and fruitful..

  • Aishwarya 9 June, 2014, 13:27

    when i run the program i get this error message:

    the requested resource is not available
    
  • Prakash K 15 July, 2014, 22:23

    Very informative , Thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current ye@r *