Spring 3 MVC – Introduction to Spring 3 MVC Framework

[ad name=”AD_INBETWEEN_POST”]

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. [sc:SpringMVC_Tutorials] 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>
Code language: HTML, XML (xml)
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.
Get our Articles via Email. Enter your email address.

You may also like...

83 Comments

  1. 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 says:

      you are always right.

    • Ratan says:

      I will give you the answer tomorrow ok…….

    • Singh says:

      I found Spring MVC complete overdone archite architecture. If you really need to see good MVC framework, go and look at portlet specifications or play around with it. You will hate Spring / Struct etc.

    • ram says:

      You are good looking

      • Annoyes says:

        ha ha ha…cooll mean

    • nari says:

      gooooooooooooooooooooood

      • naresh says:

        it’s looking very nice

    • rohit says:

      Right..its a robust and most useful framework.

    • Alok Kumar Ranjan says:

      Yes right, Spring MVC is very powerfull framework, and it is very interested framework.

  2. devdanke says:

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

  3. ganda says:

    like this tutorial , thanks..^^

  4. ibrahim says:

    Thank you so much :)

  5. michael says:

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

  6. geraldo says:

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

  7. suranga says:

    thanks so much

  8. Hai says:

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

  9. GSS says:

    Hi

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

    Thanks for your help

  10. 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

  11. Imran says:

    Very nice and helpful tutorial. Thanks for this.

  12. Assem says:

    THANK YOU MAN! TAHTS GREAT! ALL MY CHEERS!

  13. Sanjay Prajapati says:

    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…

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

  15. 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 :)

  16. Proactiv says:

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

  17. ami. says:

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

  18. mohammed rehan rizvi says:

    Good job

  19. suman says:

    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 says:

      Controller are requirement base !!!!

  20. jaydeep says:

    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.

  21. Hashim says:

    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…

  22. pseduohand says:

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

  23. starissswin says:

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

  24. kailash says:

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

  25. bernis says:

    pretty clear; lot of thanks

  26. sunil says:

    This is great work!!!!!!

  27. MWS says:

    I got through your information and using Spring.. but facing some to run. Can you provide some examples.

  28. senthil says:

    easy to follow …., can anyone know the best book for spring for beginners and also for intermediate….?

  29. Its a great tutorial, the first time i started with spring was very difficult because i dont speak english, now, i really exited with this tools. I already know what i want to be , i want to be a bakery, its a joke.

    thousands of thanks for this tutorial, i hope every one enjoy this good work

  30. Naveen says:

    Nice Tutorials. So much helpful.

  31. Deekay says:

    Great Tutorial Viral. I will surely bookmark this :)

  32. Daniel says:

    Great tutorial Viral. Thank you for this good tutorial

  33. Vijay says:

    Hi

    I am trying to develop small Spring MVC 3 application using spring-framework-3.1.2.RELEASE.zip. I downloaded this release from http://www.springsource.org/download/community. When I included the Spring jar files, my application was not running and I was getting error “Servlet dispatcher not available”. I noticed that spring-framework-3.1.2.RELEASE.zip did not had Spring MVC jar file. I included this spring mvc jar file from lower version say Spring 3.0 then my application started running.

    I have noticed the lower version and the higher version spring-3.2.0.M1-dist.zip both have spring mvc jar file then why this jar file is missing in spring-framework-3.1.2.RELEASE.zip which is the current stable version as it GA version. Please clarify.

    Thanks

    Vijay

  34. Hi,
    This tutorial was excellent and helping a lot, Can we expect the examples like

    1. Integration of Struts2, Spring 3 and Hibernate 4 ORM, JPA
    2. Apache CXF, Struts 2, Spring 3, Hibernate 4 and JPA

    Thanks & Regards,
    Sridhar Goranti

  35. Kumaraswamy says:

    It is pocket purse of all java technologies

  36. Krishna says:

    Nice tutorial for Spring beginners….

  37. Rohit says:

    Try the link below where xxxx is the project name you gave. This will start the project

    http://localhost:8080/xxxxxx/hello.html

    Remember in web.xml in the above example it is mentioned url pattern has *.html. so in the url you need to mention the url as hello.html.

    Hope this helps.

  38. krunal patel says:

    thanks ,its really helpful for fresher

  39. mukarram says:

    thanks nice tutorial

  40. Dharam says:

    Thank you for such great and Nice tutorial…………………

  41. Priya Singh says:

    Excellent tutorials for Spring MVC guys. :)

  42. Sasi says:

    Hi Viral,

    Do you have Spring core tutorials

    SC

  43. Keith says:

    HI, I’m using Spring3.0 framework.
    I have a class NODE & it contains an object of class LINK. I want to bind jsp form:input tag with Bean object of NODE. I’m writing form:input tag for property of LINK elements as link.boundaymap & link.vlanid, still bean doesn’t receive any value from jsp.
    Some binding exception still occurs. please let me know what to do.

    • better to check the commandName properly and use spring 3.0 features like go far annotations
      first you to get the form through the
      @RequestMapping(method=method = RequestMethod.GET)
      public String showForm(Map model)
      {
      //assume if you are using loginform class ,what ever the properties is there for //loginform class you have to provide same properties for jsp page also
      LoginForm loginForm = new LoginForm();

      model.put(“loginForm”, loginForm);

      return “loginForm”;
      }
      and then process the task by using post method like
      @RequestMapping(method=method = RequestMethod.Post)

  44. Mohan says:

    Hi Viral,
    Your Post is very useful for beginner. if u can include one more thing trace of the program it will be nice for beginner to understand how the program flow goes. thanks for such a useful post.

  45. give me suggesions about spring framework

  46. Its a very nioce article on Spring . Thank you

  47. manjeet rulhania says:

    very useful for beginers

  48. SHASHI says:

    Thanks viral, these are very helpful articals & examples to take a easy start with Spring framework.
    Thanks :)

    • saffique says:

      hi im new to spring mvc can u teach me how to learn from basic of spring mvc

  49. vignesh says:

    spring mvc is a worst frame work for creating a textbox i need to work on form, model, controller,service,xml… bla.. bla.. bla… in html in simple code i can create textbox

  50. Spring MVC is wonderful framework to develop a web application. I like Spring very much.

  51. Anurag says:

    Thanks… It’s informative and very methodical.

  52. yose says:

    hello!! Good!!

  53. Abdul vasih T says:

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

  54. Himanshu says:

    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.

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

  55. ashok says:

    thankx for the post it was very helpful

  56. sunder says:

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

  57. Naga says:

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

  58. gaurav says:

    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 “

  59. shrutika says:

    Thanks this was really informative!!!

  60. venkat says:

    Thanks its very Informative and fruitful..

  61. Aishwarya says:

    when i run the program i get this error message:

    the requested resource is not available
    

  62. Prakash K says:

    Very informative , Thanks a lot

  63. SS says:

    Very informative!

  64. abdul says:

    Very Nice.

  65. Pradeesh says:

    can we use two bean in a service method?

  66. Shalin says:

    Hello, What is the diagram software you used to create following diagrams? is it creately ?

  67. Nitesh Agarwal says:

    Hi,
    I am have a created a sample HelloWorld project, but after successful deployment when I am hitting the url appropriate controller is invoked but after it returns the view name status 404 is shown and in the log file error message is displayed : “No handler found”
    No mapping found for HTTP request with URI [/HelloWorld/WEB-INF/views/hello.jsp]in DispatcherServlet with name ‘spring’.
    Url I hit is “http://localhost:8081/HelloWorld/hello” and ‘spring’ is my servlet name

  68. chalapathi says:

    good

  69. Prasad says:

    Hi
    I download your source code It’s working smoothly.
    But I faced one issue when I have done following modification
    index.jsp file renamed to index.html(index.html is valid html file)
    in web.xml change welcome page as index.html and url pattern as /

    below my web.xml content
    ———————————————————————————————–
    Spring3MVC

    index.jsp

    spring

    org.springframework.web.servlet.DispatcherServlet

    1

    spring
    /

    ——————————————————————————————————–

    as soon as I tried to use .htm file as welcome page it gives me 404 resource not found error

    Can you Please comment on this behaviour .

  70. Lisa Nicholas says:

    A nice article here with some useful tips.Thanks

Leave a Reply

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