Spring MVC Interview Questions and Answers

This tutorial has popular Spring MVC Interview Questions and answers. These have been written to help you prepare for the interviews and quickly revise the concepts in general. Before reading these questions you can practice with Spring MVC tutorial with Examples. There are following selected questions related to the Spring MVC.

  1. What is Spring MVC framework?
  2. MVC is an abbreviation for a design pattern. What does it stand for and what is the idea behind it?
  3. Do you need spring-mvc.jar in your classpath or is it part of spring-core?
  4. What is the DispatcherServlet and what is it used for?
  5. Is the DispatcherServlet instantiated via an application context?
  6. What is the root application context? How is it loaded?
  7. What is the @Controller annotation used for? How can you create a controller without an annotation?
  8. What is the ContextLoaderListener and what does it do?
  9. What are you going to do in the web.xml. Where do you place it?
  10. How is an incoming request mapped to a controller and mapped to a method?
  11. What is the @RequestParam used for?
  12. What are the differences between @RequestParam and @PathVariable?
  13. What are some of the valid return types of a controller method?
  14. What is a View and what’s the idea behind supporting different types of View?
  15. How is the right View chosen when it comes to the rendering phase?
  16. What is the Model?
  17. Why do you have access to the model in your View? Where does it come from?
  18. What is the purpose of the session scope?
  19. What is the default scope in the web context?
  20. Why are controllers testable artifacts?
  21. What does the InternalResourceViewResolver do?
  22. What’s internal resource views?

1. What is Spring MVC framework?
The Spring web MVC framework provides model-view-controller 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. Read More about Spring MVC Framework.

2. MVC is an abbreviation for a design pattern. What does it stand for and what is the idea behind it?
MVC is an abbreviation name for a J2EE design pattern and it stands for Model View Controller. According to MVC design pattern in J2EE application we have three layers of architecture as Controller, Model and Service layers. A Model View Controller pattern is made up of the following three parts:

  • Model – The lowest level of the pattern which is responsible for maintaining data.
  • View – This is responsible for displaying all or a portion of the data to the user.
  • Controller – Software Code that controls the interactions between the Model and View.

MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns. Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View. The View then uses the data prepared by the Controller to generate a final presentable response. Read more about MVC design pattern.

3. Do you need spring-mvc.jar in your classpath or is it part of spring-core?
In J2EE application with Spring we required spring-mvc jar in application class path due to it provides the mvc pattern support classes. The form tag library comes bundled in spring-webmvc.jar. The spring-webmvc module (also known as the Web-Servlet module) contains Spring’s model-view-controller (MVC) and REST Web Services implementation for web applications. Spring’s MVC framework provides a clean separation between domain model code and web forms and integrates with all of the other features of the Spring Framework.

Spring-mvc.jar is not part of spring core because the Core Container consists of the spring-core, spring-beans, spring-context, spring-context-support, and spring-expression (Spring Expression Language) modules.

4. What is the DispatcherServlet and what is it used for?
In Spring MVC framework Dispatcher Servlet access Front Controller which handles all coming requests and queues for forward to the different controller. Front controller is a typical design pattern in the web applications development. In this case, a single servlet receives all requests and transfers them to to all other components of the application. The task of the DispatcherServlet is send request to the specific Spring MVC controller.

DispatcherServlet is responsible for initialize the WebApplicationContext and it loads all configuration related to the web components like controllers, view resolver, interceptors etc., It will be loaded and initialized by calling init() method init() of DispatcherServlet will try to identify the Spring Configuration Document with naming conventions like “servlet_name-servlet.xml” then all beans can be identify. Read more about DispatcherServlet in Spring MVC.

5. Is the DispatcherServlet instantiated via an application context?
No. ApplicationContext instantiated via DispactcherServlet.

6. What is the root application context? How is it loaded?
In spring mvc for every web application applicationContext.xml file used as the root context configuration. Spring loads this file and creates the ApplicationContext for whole application. This file applicationContext.xml is loaded by ContextLoaderLoaderLinstner which is configured into web.xml file as the context configuration. Read more about Root Application Context and WebApplicationContext in Spring MVC.

7. What is the @Controller annotation used for? How can you create a controller without an annotation?
In Spring MVC framework @Controller used for creating controllers for the web application. It is meta annotation of @Component annotation to create the bean by spring container.

The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API. Read more about @Controller, @Repository and @Service stereotype annotations.

8. What is the ContextLoaderListener and what does it do?
This listener is responsible to load the context configuration files. It performs the actual initialization work for the root application context. It reads a “contextConfigLocation” context-param and passes its value to the context instance. We can pass multiple files in the context configuration by commas or space separation. e.g. “WEB-INF/applicationContext.xml, WEB-INF/applicationContext-security.xml”. Read more about ContextLoaderListener in Spring MVC.

9. What are you going to do in the web.xml. Where do you place it?
For Spring MVC application we have to do following entry in web.xml file which place under the WEB-INF folder.

A. Configure spring dispatcher servlet

<servlet>
  <servlet-name>webapp1</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>

 <!-- We require this configuration when we want to change the default 
 name / location of the servlet specific configuration files -->
 <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/app1-servlet.xml</param-value>
 </init-param>
 <load-on-startup>0</load-on-startup>
</servlet>

B. Configure Spring ContexLoaderListner but it is optional

<!-- This is the root application context for whole web application. -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/rootApplicationContext.xml</param-value>
</context-param>
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

10. What is @RequestMapping? How is an incoming request mapped to a controller and mapped to a method?
@RequestMapping can be applied to the controller class as well as methods. This annotation for mapping web requests onto specific handler classes and/or handler methods. Read more about @RequestMapping annotation in Spring MVC.

11. What is the @RequestParam used for?
In spring the @RequestParam annotation is used to bind request parameter values to the handler method arguments in controller.

@Target(value=PARAMETER)
@Retention(value=RUNTIME)
@Documented
public @interface RequestParam
  • defaultValue-It is String type attribute and the default value to use as a fallback when the request parameter is not provided or has an empty value.
  • name-It is String type attribute and name of the request parameter to bind to.
  • required-It is boolean type attribute whether the parameter is required.
  • value– It is String type attribute and it is alias for name attribute.

Read more about @RequestParam annotation in Spring MVC.

12. What are the differences between @RequestParam and @PathVariable?
@PathVariable: Is is used to pass parameter along with the url, sometimes we need to pass parameters along with the url to get the data. Spring MVC provides support for customizing the URL in order to get data. To achieving this purpose @PathVariable annotation is used in Spring mvc framework.

@RequestParam: It is used to get the request parameters. @RequestParam automatically binds the request parameters to the arguments of your handler method. It also provides auto type conversion for some standard type like int, long, float, string, date etc.Read more about differences between @RequestParam and @PathVariable annotation in Spring MVC.

13. What are some of the valid return types of a controller method?
There are many return types are available for Handler method which is annotated by @RequestMapping inside controller like :

  • ModelAndView (Class)
  • Model (Interface)
  • Map
  • String
  • void
  • View
  • HttpEntity<?> or ResponseEntity<?>
  • HttpHeaders

and much more…..See Docs

14. What is a View and what’s the idea behind supporting different types of View?

  • A View renders web output.
  • Many built-in views available for JSPs, XSLT, templating approaches (Velocity, FreeMarker), etc.
  • View support classes for creating PDFs, Excel spreadsheets,etc.
  • Controllers typically return a ‘logical view name’ String.
  • ViewResolvers select View based on view name.

15. How is the right View chosen when it comes to the rendering phase?
View Resolvers

  • The DispatcherServlet delegates to a ViewResolver to obtain View implementation based on view name.
  • The default ViewResolver treats the view name as a Web Application-relative file path – i.e. a JSP: /WEB-INF/reward/list.jsp
  • Override this default by registering a ViewResolver bean with the DispatcherServlet
    • We will use InternalResourceViewResolver
    • Several other options available.

16. What is the Model?
Model in Spring MVC has processed data to be rendered in the view by the respective view resolver. Model has two part Service and Persistent.

17. Why do you have access to the model in your View? Where does it come from?
In the Spring MVC model has data to be rendered front to the user so spring provide support to access the model in the view layer. This model is passed by the controller to the dispatcher servlet and dispatcher server send it to the respective view resolver for rendering model’s data to the view and generate html page upfront to user.

18. What is the purpose of the session scope?
This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

The idea here is that you have a web application, and you have various objects that exist on a per-session basis, such as maybe a user profile or a shopping cart. You’d like to make those available to some service bean, say, without having to manually pull them off the session and pass them as method arguments every time.

19. What is the default scope in the web context?
singleton

20. Why are controllers testable artifacts?
Spring provides support for mockito framework for testing. Mockito provides MockMVC class is the main entry point of our tests. We can execute requests by calling its perform method.

21. What does the InternalResourceViewResolver do?
InternalResourceViewResolver is one of the implementation of ViewResolver for JSP templates. It is used to resolve “internal resource view” based on a predefined URL pattern. In additional, it allow you to add some predefined prefix or suffix to the view name (prefix + view name + suffix), and generate the final view page URL.

22. What’s internal resource views?
Those views under “WEB-INF” folder are named as internal resource views, as it’s only accessible by the servlet or Spring’s controllers class. In Spring MVC web application, for good practice, it’s always recommended to put the entire views or JSP files under “WEB-INF” folder, to protect it from direct access via manual entered URL.

Previous
Next