What is Spring MVC?

Spring MVC is the module of Spring that implements the front controller and MVC design pattern. It provides a decoupled approach to developing web applications.

In Spring MVC, the three components of the MVC architecture, the front controller, view resolver and model are not dependent on each other. All incoming requests are handled by the front controller which is called the DispatcherServlet.

What are the advantages of using Spring MVC?

  • Spring MVC is based on interfaces which are independent of each other allowing separation of concerns.
  • Because there is no explicit dependency between the interfaces, Spring MVC applications are easily testable.
  • The view technology is customizable and switching between JSP, Velocity or Thymeleaf etc. can be done easily.
  • Spring MVC supports RESTful web services.

What is the flow of request in the MVC architecture?

The dispatcher servlet is the front controller which receives all requests from the client. It has a mapping of all controllers and maps the incoming request to the appropriate controller.

The controller executes the request and returns a model and view name to the dispatcher servlet. The model contains the results.

The dispatcher servlet uses the view resolver to resolve the name of the view and populates it with results from the model and displays it to the client as a web page.

What are the steps required to create a Spring MVC application?

To create a simple MVC application, we perform the following steps:

  • Include the spring-context and spring-webmvc dependencies in the pom.xml file.
  • Define the dispatcher servlet in web.xml to handle all requests.
  • Configure the controller classes using XML or annotations.
  • Create URL mappings in controller classes to handle incoming requests.
  • Configure a view resolver.

Is spring-mvc jar included in spring-core?

spring-mvc jar is not a part of spring-core. It needs to be added separately to the classpath. It has a dependency on spring-core jar which is downloaded automatically if a build tool like Maven is used.

What is the Front Controller Pattern?

Front Controller Pattern is a design pattern in which request handling is centralized. All client requests go to the front controller which contains a mapping of all controllers/servlets. The front controller maps the client request to the relevant controller/servlet, receives the results back, and renders the view to the client.

In Spring DispatcherServlet is the front controller. It receives all the requests from the client and forwards them to the appropriate controller.

Describe the function of the DispatcherServlet.

The DispatcherServlet is at the heart of Spring MVC. It is the front controller which handles all the HTTP requests and responses.

The dispatcher servlet uses a configuration file containing information of all controllers and the URL mappings to forward an incoming request to the correct controller. It gets a model object and view back from the controller. The dispatcher servlet then forwards the view name to the view resolver and renders the response back to the client.

How is the Dispatcher servlet configured?

Dispatcher servlet can be configured using XML or programmatically using the ServletContainerInitializer interface.

  • XML configuration: The dispatcher servlet is configured in the web.xml file like any other servlet. The <servlet-name> tag specifies the name, while <servlet-class> tag specifies the class as org.springframework.web.servlet.DispatcherServlet.

    To direct all requests to go through the dispatcher servlet, the <servlet-mapping> tag is used with the /* URL mapping. The <load-on-startup> tag with value 1 is used to preload the dispatcher servlet, otherwise it is loaded when a request comes.

  • Java configuration: Spring provides its own implementation of the ServletContainerInitializer interface. We can use the class AbstractAnnotationConfigDispatcherServletInitializer and provide implementation of the getServletConfigClasses and getServletMappings methods to configure the dispatcher servlet.

How does the dispatcher servlet map a request to a controller method?

The dispatcher servlet uses handler mappings and annotations like @Controller and @RequestMapping to map a request to a controller method.

The HandlerMapping interface provides a mapping between requests and handler objects. Spring provides implementations of this interface where incoming request URLs can be mapped to controller classes.

What is a controller?

A controller is a class that handles user requests. It is annotated with the @Controller annotation.

A controller class contains the business logic of the application. Based on the client request, a controller populates the model which is shown to the user as a view.

What is the function of @Controller annotation?

The @Controller annotation is used at class level. It marks the class as a controller class and tells the Spring framework that this class will handle user requests.

What is the difference between @Controller and @RestController annotations?

Web applications and REST APIs differ in their return types with the former returning a view comprising of HTML, CSS and JavaScript while the later returning JSON or XML data.

The @Controller annotation populates the model map and returns a view name that is sent to the view resolver. The @RestController annotation returns an object which is written to HTTP response as JSON or XML.

The @RestController annotation is a combination of @Controller and @ResponseBody annotations. To return JSON or XML in a web application, we need to use the @ResponseBody annotation explicitly with the @Controller annotation.

What is the function of @RequestMapping annotation?

When the @RequestMapping annotation is used at class level, it acts as a parent mapping which maps the URL of an incoming request.

When it is used on method level, it maps the URL as well as the HTTP request method.

When using @RequestMapping annotation, can multiple paths map to the same controller method?

Usually, the @RequestMapping annotation is used to map a single path to a controller method but this is not a rule. Multiple mappings can be specified in the value attribute as can be seen from the following example:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.