Spring MVC Framework

The Spring Web MVC framework provides Model-View-Controller (MVC) architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.

  • The Model encapsulates the application data and in general they will consist of POJO.
  • The View is responsible for rendering the model data and in general it generates HTML output that the client’s browser can interpret.
  • The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.

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.

@Controller
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.

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

View:-

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"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<body>
Message : ${msg}
</body>
</html>

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:-

@Configuration
public class MyWebConfig {

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

viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}

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