Spring MVC Framework


  • The Model encapsulates the application data and in general they will consist of POJO.

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that handles all the HTTP requests and responses. The request processing workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram −

A spring controller is a simple Java class. This class is annotated with @Controller on the class level. The controller and/or its methods are mapped to request URI using @RequestMapping. The controller’s methods are usually known as handlers. There can be multiple controller classes in an application. A controller is registered as a bean. That means we can inject any other bean/service there. The default scope of a controller is singleton.

public class MyController {
@RequestMapping(value = "/my-handler-path", method = RequestMethod.GET)
public String myHandlerMethod(...) {

org.springframework.ui.Model serves as MVC model. It binds the view attributes with application specific values. If the handler method parameters list has Model type, its instance is passed by Spring.

public class MyMvcController {
@RequestMapping(value = "/my-uri-path")
public String prepareView(Model model) {
model.addAttribute("msg", "msg-value");


Spring framework comes with many views. One of the commonly used views is JstlView, which expects JSP resources be provided at specified location under webapp directory. That means a developer responsibility is to provide a JSP file. A JSP file uses model’s provided attribute. for example:

<%@ page language="java"
Message : ${msg}

What is ViewResolver?

ViewResolver is an interface. Typically, the implementation of this interface is responsible to resolve views by name. We don’t have to implement it ourselves as there are already many view resolvers available. One of the commonly used view resolvers is InternalResourceViewResolver, it's default View is JstlView, which can be changed. We can configure multiple view resolver in an application.

we can configure view resolver as a bean:-

public class MyWebConfig {

public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver =
new InternalResourceViewResolver();

return viewResolver;

In above configuration the Jstl resources must be located under /WEB-INF/views/ directory and must have file extension of jsp.