Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

The Curious Coder’s Guide to Java Web Frameworks: Spring MVC

Get the full report

Psst! Want to see the 8 most popular Java Web Frameworks compared? Since this post here, we’ve made The Curious Coder’s Java Web Frameworks Comparison: Spring MVC, Grails, Vaadin, GWT, Wicket, Play, Struts and JSF. Grab the complete report in HTML and/or PDF. Win!

Dude, go for the Full Report instead!


This is the third post in our review of Java web frameworks, having already covered Vaadin and Grails. Today we look at Spring MVC, a web framework and module of the larger Spring Framework, also by Pivotal (like Grails). According to our latest Developer Productivity report, Spring MVC is the most commonly used web framework among Java developers, accounting for 30% of the market.

Welcome to the Spring Framework, and the world of configuration! Even using Spring MVC, which is just one of several modules in the Spring Framework, won’t get you off the hook. Everything starts off from configuration, and we’re not just talking about the web.xml here. You need to know how Spring as a whole works, which is the biggest caveat when using Spring MVC for your web app.

Probably the easiest way to set it all up is to download the Petclinic package, strip out all unnecessary stuff (if you know, what is necessary and what not) and start from there.

But there are way smarter ways to start off with Spring MVC, like tutorials and online documentation pages. And this is actually where it all more or less begins – I needed to spend more than half of my day to read through the documentation in order to understand the framework. And this doesn’t mean that I had it working.

I think this is the point that Spring MVC and Spring is generally missing – many developers tend to take a framework that is easier to bootstrap with.

What is Spring MVC?

Spring MVC is part of the huge Spring Framework stack that contains other Spring modules. Spring MVC is really just a Spring module, which means that you can’t run it without Spring, but you can run the Spring Core without Spring MVC.

The framework architecture is quite straightforward–it is basically a DispatcherServlet passing requests to appropriate controllers and mapping data to models (the picture below is stolen from Spring’s website, and redesigned for coolness by the ZeroTurnaround team):

Spring MVC can have different types of views in use, which is cool. So you can have regular JSP templates or even an entire framework for rendering views, like Struts. This can be configured using View Resolvers.

Getting Started

The easiest way to get the necessary jar-s is to download the Spring Framework’s distribution bundle from the downloads section of the website. But there are other alternatives as well, like using Maven for resolving the following dependencies:

  • Spring Core

  • Spring Web

  • Spring MVC

Spring MVC in action

Unfortunately, it was quite hard to understand Spring MVC quickly, namely with how to set it all up and get it running. So we searched for “Spring MVC tutorial” and found loads of tutorials showing an easier way to grasp than the Spring documentation itself.

The easiest way to not get lost in the bushes of XML and get things working is to do as follows:

  1. Set up the basic skeleton of a regular Java web app

  2. Configure Spring dependencies (Core, Web and MVC)

  3. Configure Spring (set up basic application context configuration)

  4. Configure Spring MVC

  5. You are ready to go

As Spring MVC relies on the DispatcherServlet, then it is needed to set it up in web.xml as well (of course, Spring configuration must be in place too):

  <servlet>
    <servlet-name>mvctest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvctest</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

Annotations

One of the coolest feature in Spring MVC is annotations. Code less and declare more!

Annotations enable to access servlet context, different variables and data that is moving back and forth between the end user and the web app.

It is possible to use Spring Core annotations like @Autowired and Spring MVC annotations at the same time in the single controller class. This makes configuration painless and very easily manageable. Annotations enable to connect a controller with the outside and inside world in the way that users want.

@Controller
@RequestMapping("/path/{myVariable}")
public class MyController {
 
    private final MyService myService;
 
    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
    }	
 
    @RequestMapping("/mypath/{myPathVariable}")
    public void findPet(@PathVariable String myVariable, @PathVariable String myPathVariable, Model model) {    
      // TODO
    }
}

Extendability

As the Spring Framework is very configurable by using lots of interfaces and configuration files, then the same applies to Spring MVC.

As told before, Spring MVC provides quite universal ViewResolver interface. This can be used for using different UI framework in addition to regular JSP pages or for formatting content for the end user, like providing XML output with XmlViewResolver.

<!-- - This bean configures the 'prefix' and 'suffix' properties of -->
<!-- InternalResourceViewResolver, which resolves logical view names - returned by Controllers. -->
<!-- For example, a logical view name of "vets" - will be mapped to "/WEB-INF/jsp/vets.jsp". -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2" />

Time to express our feelings

Our first impression is that using Spring MVC to create a simple web app is a little bit overkill. You’ll need to spend some time with the configuration and understanding of the overall Spring Framework. However, for those building serious applications with a solid foundation, nice user interface and a RESTful API available for the outside world, then it is a good choice.

Probably there are many people who would use it for everything, because in general Spring is a very configurable and extensible framework and these properties are also inherited by Spring MVC.

Although Spring MVC has a very rich feature set to develop and maintain code on the server side, it still doesn’t provide any rich framework for building awesome and cool user interfaces. With that being said – if you want to write awesome backend and awesome frontend, use Spring MVC for the first and tie another cool framework intended for building rich UIs for the second part, such as Angular JS.

P.S. You can contribute

After spending a little time with Spring Framework creator Jürgen Höller at our recent Estonian technology conference GeekOut, we were reminded that even through the Spring Framework is a large and very enterprise framework with a lot of history, it is still open for contributions from anyone and everyone. The folks at SpringSource strongly encourage you to fork their project repositories in Github and submit pull requests. If your contribution is significant enough, it is possible to get more involved as an Individual Contributor.

 

  • Manuel Eguiluz

    Thanks for the post. Good review.