March 7, 2015

Spring MVC fundamentals

Spring MVC

Other framworks

  • Struts
  • Tapestry
  • Wicket
  • GWT
  • JSF
  • Seam
  • Stripes

What is Spring MVC

  • General Web (JSP/REST/Headless/Remoting) framework
  • POJO based and interface driven
  • Based on Dispatcher Servlet / Front Controller pattern


  • DispatcherServlet – Entry point for the application
  • Controller – Command pattern object that handles request and routes to a view
  • RequestMapping – URL and Request Type that method is tied to (GET/PUT/POST…)
  • ViewResolver – Used to locate JSP pages or whatever view we are using
  • Servlet-config – Configuration per DispatcherServlet

Spring MVC Configuration

  • Configure web.xml
  • Configure servlet-config.xml
  • Add Controller
    • Controllers are annotation based using @Controller
    • Controller path is set using annotations @RequestMapping
  • Add View
    • Put views under WEB-INF/jsp/*.jsp and redirect using InternalResourceViewResolver




Controller (@Controller)

  • Routes requests
  • Builds response
  • Handles exceptions
  • Recommended to put request mapping on methods

Service (@Service)

  • Handles actions of a system
  • Service contains business logic
  • Ensures the business object is in valid state
  • Transactions begin here
  • Often same method as Repository but different focus (set default etc.)

Repository (@Repository)

  • Repository accesses data through database
  • Usually mapped 1-1 with db-tables
  • Focuses on persisting and interacting with database (CRUD)

Request/Response lifecycle

  1. Incoming request -> Front Controller -> Delegate Request -> Controller -> Handle Request -> Backend
  2. Backend -> Create Model -> Controller -> Delegate Rendering (Model) -> Front Controller -> Model -> View Template -> Front Controller -> Return Response

Passing params

  • Request[‘params’] -> Controller[‘model’] -> Response
  • Model used for both get/post to database
  • Use @ModelAttribute to send data or retrieve data to/from Controller
  • Use with GET/POST on POJOs


  • Convention to place views in /WEB-INF/jsp/ directory
  • Allows us to completely control all routing, can make site more secure
  • View Resolver uses string returned from controller method
  • Controller can build model and return to View Resolver
  • Many different resolvers for different templating tools and return types
  • Possible to create static view resource mapping to publish images, pdf’s etc.

Tag Libraries

  • Two libs in Spring MVC
  • Can bind to objects from model
  • Can connect to css
  • Can escape data
  • Spring ex.
    • bind
    • escapeBody
    • hasBindErrors
    • htmlEscapt
    • message
    • nestedPath
    • theme
    • transform
    • url
    • eval
  • Spring Form ex.
    • checkbox(es)
    • errors
    • form
    • hidden
    • input
    • label
    • option
    • password
    • select


  • Registered and part of request lifecycle
  • Have ability to pre/post handle web requests before and after controller
  • Callback methods used to override or change values
  • Commonly used for locale changing


  • Constraint validation
    • Expecting data like “firstname”, “password” must not be null and contain some specific data
  • Business logic validation
    • Rather constraints on what values are handled
    • Should be handled i service tier, not controller
  • Form tags have an error class for displaying errors
  • Validator interface
    • ValidationUtils helper class used with simple form controller
    • Implements Validator Interface
    • Requires lots of hands on coding for validation
    • Binds to a BindingResult
    • Can be used in Service tier to do things outside of spring
    • Can use SimpleFormController
    • Limited use and not annotation supported
  • JSR 303 validation
    • Java standard for validation
    • Integrates well with Spring for validation and reporting errors
    • Annotation based and can use custom validation rules
    • Reference implementation is the Hibernate Validator (not same as Hibernate ORM!)
    • POJO based

Application request types

  • Can set up to handle json and xml etc.
  • Configure in servlet xml
  • Dispatcher Servlet needs to be configured to allow different request types