Lets set up Spring MVC

Welcome to the Modern Web, here’s your library. Spring MVC comes standard with the monolith that is the Spring Framework. It certainly makes creating a web app easy… once it’s set up. Getting started, though, is the difficult part.

This isn’t meant to be a very educational article, only an instructive article. If you are unfamiliar with the concepts listed, google is your friend. There are a lot of moving pieces to this, so lets dive in.


What do we want to accomplish? We want to set up Spring MVC to handle all *.html files, not interact with resources such as javascript and CSS files, forward rendering to JSP files under WEB-INF/jsp and use Logback for logging.


There’s one downside for Spring, a lot of jars are involved. To accomplish the above we need the following jars:


Your versions may vary

File Structure

We will order our files in the following directory structure:

    |   +---groovy
    |   |   \---com
    |   |       \---mycom
    |   |           \---<controller>.groovy
    |   \---resources
    |       \---logback.xml

<controller>.groovy will hold the controller code that will load the model and forward to the <view>.jsp under WEB-INF/jsp. logback.xml will configure our logging to print out all the debug statements to make sure the app is working correctly. css, images and js hold the appropriate resource files. spring-servlet.xml configures Spring MVC to find our controllers and views. And web.xml is the standard deployment descriptor.


The deployment descriptor sets up 4 items: the welcome files list, set to index.html, default HTML escaping to prevent XSS attacks, the DispatcherServlet, and which files Spring MVC will handle.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">

  <!-- Escape HTML output by default -->

  <!-- This servlet named "spring" will make Spring MVC look for
       spring-servlet.xml under WEB-INF -->

  <!-- Map Spring MVC to all .html files -->


The *-servlet.xml file is automatically loaded by the DispatcherServlet based on the servlet name. In this case, Spring will look for spring-servlet.xml. We need to configure this file to do the following: scan our source code for controllers, note that Spring MVC will be driven by annotations (not necessarily needed, but will prevent issues in the future) and tell Spring MVC to resolves views in our WEB-INF/jsp folder.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <!-- All annotated Spring classes under this package will be picked up -->
  <context:component-scan base-package="com.mycom"/>
  <!-- Drive Spring MVC with annotations -->
  <mvc:annotation-driven />

  <!-- Any view returned by the app will be resolved to WEB-INF/lib/[view].jsp -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>


Since Spring MVC is mapped to *.html, only html files will be picked up. So any images, CSS and javascript can be placed in their respective folders and be picked up with out being processed.


Any groovy files with the @Controller annotation will be picked up Spring. The controller needs to hold methods that map to a specific URL (an html file, in this case). It will then need return a Model and or View. The string representing the view will be mapped to the corresponding JSP file under WEB-INF/jsp.

package com.mycom.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.servlet.ModelAndView

// Ensure Spring picks this class up as a controller
class DefaultController {
  // map index.html to WEB-INF/jsp/index.jsp
  def index() {
    return 'index'

  // map search.html to WEB-INF/jsp/search.jsp with a SearchResults as the model
  def search() {
    return new ModelAndView('search', 'results', new SearchResults(...))


Our views a simple JSP pages that get resolved to WEB-INF/jsp/.jsp. Since this is just an article about setup, you will need to see the documentation for more information.


Lastly we configure our logging to print out all our debug statements, letting us know if everything is working correctly.

<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="true">
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n

  <logger name="org.springframework" level="DEBUG"/>
  <root level="DEBUG">
    <appender-ref ref="STDOUT" />
Jay Lindquist avatar
About Jay Lindquist
Developer and wonderer