Introduction to Spring MVC

Spring MVC is a web application framework for developing applications using Enterprise Java or J2EE as it’s commonly referred to. Spring MVC is a very unobtrusive framework. It is lightweight in a sense that there’s very little overhead in running it but it’s a very capable, heavy duty framework. It pulls a lot of best practices from all of the other frameworks listed below and combines it into Spring MVC. Spring MVC doesn’t try to niche itself for one particular type of web application. It can be RESTful based. It can be JSP based. We can use other view technologies like FreeMarker or Velocity. It can be used for headless applications. It can also be used as a remoting framework. All these capabilities are built into Spring MVC.

Other Frameworks include

  1. Struts 2
  2. Tapestry, one of the first heavily object-oriented or POJO-based web frameworks.
  3. Wicket is a lightweight framework  that sells itself on being easier to configure than some of the other frameworks out there.
  4. Google Web Toolkit (GWT) is a very rich user experience framework with a steeper learning curve than a lot of the other frameworks that are out there.
  5. JSF from Oracle – the only true standard framework for the web out there is J2EE based and it’s a very rich user experience. It comes with libraries such as Ice Faces and Rich Faces and other third party components for integrating into JSF.
  6. Seam is a JSF framework from the JBoss Group.
  7. Stripes is a very light weight web framework that has very similar features to Spring MVC. It’s pretty much a view technology framework.

What is Spring MVC?

  • A web framework built around the principles of Spring.
  • POJO based and interface driven design.
  • It can be Unit tested very easily.
  • Based on a Dispatcher Servlet / Front Controller Pattern.
  • Very lightweight and unobtrusive compared to other frameworks. Doesn’t weigh you down with things that you don’t need. With Spring MVC you can just take the little pieces that you want and just use those.
  • Built from the shortcomings of Struts 1.
  • Support for:
    • Themes
    • Localization/i18n
    • RESTful Services – Easy to dump RESTful data out of Spring MVC.
    • Annotation based Configuration. You don’t have to use  Interfaces in their code if you don’t want to. There are things that you can get by following their convention over their configuration. So if we mark some things up with annotations, that works very nicely.
    • Seamless Integration with other Spring Services/Beans. The whole dependency injection module works very well with all of Spring, especially with Spring MVC.

Architecture

Spring MVC is built on top of the Java Servlet API/J2EE. We write our application on top of or using Spring MVC.

Request/Response Life Cycle

We have an incoming request that hits our Servlet Front Controller/Dispatcher Servlet inside of Spring. So that hands off the request, or in other words delegates our request over to one of our controllers that we have set up. The controller handles the request and routes it to the back end. The back end could be composed of web services or a database or multiple databases or any number of things that we are gathering data from. And in turn, what that does is it hands back a model to us. The Model is basically just our data, that we are trying to represent on our screen. Once that gets back to the controller, it delegates the rendering to the Front Controller. Rendering is separate from business logic. Coming back from that controller, we have a model now or data that we are going to represent the UI. The Front Controller passes the model down to some view template. Once it’s gone down to our view template, our JSP page, we are going to return control back to our Front Controller and then return back our response to the browser.

Vocabulary

  • DispatcherServlet – The entry / configuration point  for the application. It also happens to be our Front Controller.
  • Controller – Command Pattern object that handles the request and determines which view to route to and what model we should get and handles that information and hands it back and forth.
  • RequestMapping – The RequestMapping is our URL and the request type (POST, GET, PUT, DELETE) that our method is tied to.
  • ViewResolver – Used to locate JSP pages or whatever view technology we are using.
  • Servlet-config – A configuration file per DispatcherServlet. This is where Spring knows to configure things and wire our application up.
  • POJO – Plain Old Java Object, generally has no arguments constructor and private member variables exposed through getters and setters.
  • Bean – A Spring configured POJO. It’s a POJO that we have instantiated through Spring, actually in our Servlet-config and it’s wired up to be used inside of our framework.

Summary

  • Spring is J2EE based.
  • Built around best practices and design patterns. Specifically the Front Controller Design Patterns and its MVC.
  • Has a very stable release cycle. Every 18 months to almost two years.
  • Backwards compatible. Also it’s actually very forwards compatible. Applications that you have written in an older version can usually be upgraded quite easily towards the newest version.
  • Very active community. Spring itself is the most common, most popular framework inside the Java Community.
  • Lightweight and unobtrusive framework compared to other frameworks out there like JSF, GWT.
  • Flexible to use. We can have it exposed to web services. We can tie it to a JavaScript UI exposed through JSON services, JSON RESTful services. We can also dump out standard HTML or we can use a different view technology templates like Freemarker, Velocity.
  • Dump this out to a mobile application just as easily and not change anything on your back end.