Spring MVC

The Spring MVC Framework offers a simple interface based infrastructure for handing web MVC architectures. In most cases a Spring MVC application is quite testable because Spring does not require the developer to extend a base abstract actions/controllers a la Struts.

There are many other Java-based web frameworks out there (Struts, WebWork, various JSF implementations, Tapestry, etc.) and all have their pros and cons, but if you are already using the Spring Framework for other services, an added benefit of using Spring MVC is that other Spring Beans can be easily injected into the web controllers. If your services injected into your controllers are interfaces, it is then very easy to write alternate simple implementations of those interfaces for the purpose of testing your Controllers.

Key interfaces:

  • Controller — Must implement ModelAndView handleRequest(request,response) — This is the base controller interface, comparable to the notion of a Struts Action.
  • View — Must implement void render( model, request, response) This is the MVC view for a web interaction. Implementations are responsible for rendering content, and exposing the model.
  • To complete the MVC trio, note that the model is typically handled as a java.util.Map which is returned with the view, and the values are available, for example in a JSP, using a <jsp:useBean/> where the id corresponds to the key value in the model (Map).

Spring MVC is designed with a clean interface-based design. The key interfaces to understand are View and Controller. The trickiest part of using Spring MVC is probably wiring everything up, but once you go through this exercise once, it is really quite simple. There is a lot more to Spring MVC. Some subjects will be detailed in later articles. Check out the Spring JavaDocs and the resources listed below for more information.