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.



28 Comments

  • Java Development wrote on 22 June, 2010, 5:03

    SPRING MVC is really powerful framework. I would be interested to see this working with the Spring Webflow which we have been using for long time now with older version of spring.

    • vikas wrote on 25 October, 2011, 11:27

      you are always right.

    • Ratan wrote on 19 January, 2012, 17:22

      I will give you the answer tomorrow ok…….

  • devdanke wrote on 22 June, 2010, 7:21

    This tutorial is just what I need and just when I need it. I hope the rest of this series is coming soon. Thanks.

  • ganda wrote on 8 September, 2010, 9:20

    like this tutorial , thanks..^^

  • ibrahim wrote on 8 October, 2010, 18:04

    Thank you so much :)

  • michael wrote on 10 October, 2010, 3:17

    This is a great serious of tutorials. It definitely helped me to get going with spring 3.0.

  • geraldo wrote on 13 December, 2010, 19:20

    Excellent tutorial. Well done. I got it working first time round. Thanks so much.

  • suranga wrote on 16 December, 2010, 9:13

    thanks so much

  • Hai wrote on 2 January, 2011, 21:44

    Thank you so much. This is an excellent tutorial. I can get myself familiar with Spring 3 quickly after looking this tutorial.

  • GSS wrote on 25 January, 2011, 4:13

    Hi

    If you have worked with Struts2, is it possible for you to recommend Struts2 or Spring3 MVC?

    Thanks for your help

  • Javin @ Tibco RV Tutorial wrote on 6 February, 2011, 16:24

    Very nice article dude.
    What I like in spring is there IOC support , AOP support and various template they have provided for enterprise integration e.g. JMS Template , JDBCTemplate etc.
    Thanks
    Javin

  • Imran wrote on 8 March, 2011, 13:01

    Very nice and helpful tutorial. Thanks for this.

  • Assem wrote on 15 March, 2011, 16:33

    THANK YOU MAN! TAHTS GREAT! ALL MY CHEERS!

  • Sanjay Prajapati wrote on 15 March, 2011, 16:35

    Hay man,

    really you are genious, you really worked very good,
    your each n every article is so simple and good to understand.

    is it possible to make any example in spring MVC and Struts2 and Hibernate???

    Thanks for evrything…

  • Sudhir Nayak wrote on 23 March, 2011, 10:08

    Hi this is very useful tutorial to help the clear concept and easy configration .

  • Maxim Dsouza wrote on 28 March, 2011, 17:18

    Nice work there…. An in-depth and through article.. will really be helpful. Im a avid spring programmer and I really like watever is mentioned here… good job :)

  • Proactiv wrote on 28 April, 2011, 13:02

    Friend, I am interesting what the widget you are using. Thank you sir forgive my english.

  • ami. wrote on 28 April, 2011, 23:53

    Spring MVC is new for me, but is very easy with this tutorial, thank you !

  • mohammed rehan rizvi wrote on 7 June, 2011, 18:58

    Good job

  • suman wrote on 23 June, 2011, 10:38

    Hi ,

    Thanks for the Very good article.

    I would like to know which Controller to select in my application.
    If you have any info in Deciding the Controllers it would be still great.

    Regards,
    Suman

    • Mayank wrote on 11 November, 2011, 18:46

      Controller are requirement base !!!!

  • jaydeep wrote on 24 August, 2011, 7:16

    I always looks for this type of tutorials while i start to learn something new. It definitely helped me to get going with spring 3.0.This is a great serious of tutorials.
    Thanks for all.

  • Hashim wrote on 30 August, 2011, 12:23

    This is a great tutorial. It is easy to understand. I get familiar with w/ spring when I read this.. Keep up the gud work…

  • pseduohand wrote on 4 September, 2011, 8:34

    Thx for the tutorial. It gave me a lot help.

  • starissswin wrote on 17 November, 2011, 16:18

    Thanks 4 giving only the examples that matters most effectivly . keep up the good work

  • kailash wrote on 6 January, 2012, 15:48

    your explaination way always great .whatever the technology you are the best one

  • bernis wrote on 10 January, 2012, 23:11

    pretty clear; lot of thanks

Leave a Reply

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

*

Copyright © 2012 ViralPatel.net. All rights reserved.