Spring MVC Interview Questions and Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7
At a glance
Powered by AI
The key takeaways are that Spring MVC provides model-view-controller architecture for developing web applications and separates different aspects of the application.

Spring MVC is a framework that provides model-view-controller architecture and components to develop flexible and loosely coupled web applications. It separates input logic, business logic and UI logic.

MVC stands for Model View Controller. It is a design pattern that separates an application into three main logical components - the model, the view and the controller. The model manages the behavior and data of the application, the view manages the display of information and the controller accepts input and converts it to commands for the model and view.

2/15/2017 Spring MVC Interview Questions and Answers | Dinesh on Java

Home Core Java Spring Spring Boot Hibernate Struts 2 J2EE Web Services Hadoop Design Patterns Maven MongoDB

Dinesh
3,134 likes

Spring MVC Interview Questions and Answers Like Page

Posted by Dinesh Rajput
Be the first of your fri
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.
LABELS
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? About My Professiona
AJAX (4)
3. Do you need spring-mvc.jar in your classpath or is it part of spring-core? Ant (11)
4. What is the DispatcherServlet and what is it used for? cloud computing
collection (18)
5. Is the DispatcherServlet instantiated via an application context?
Core JAVA 
6. What is the root application context? How is it loaded? Core Java Interview Q
Garbage Collection
7. What is the @Controller annotation used for? How can you create a controller without an annotation?
GitHub (3)
8. What is the ContextLoaderListener and what does it do? Gradle (3)
9. What are you going to do in the web.xml. Where do you place it? Hadoop (14)
Hibernate (14)
10. How is an incoming request mapped to a controller and mapped to a method? hibernate4 
11. What is the @RequestParam used for? interview questions
Java (3)
12. What are the differences between @RequestParam and @PathVariable?
JavaMail (14)
13. What are some of the valid return types of a controller method? JAXB (4)

14. What is a View and what's the idea behind supporting different types of View? JDBC (1)
JSP (29)
15. How is the right View chosen when it comes to the rendering phase? JSTL (44)
16. What is the Model? Linux (2)
Logging (2)
17. Why do you have access to the model in your View? Where does it come from?
maven (12)
18. What is the purpose of the session scope? Microservices
migration (1)
19. What is the default scope in the web context?
mongodb (18)
20. Why are controllers testable artifacts? motivational
21. What does the InternalResourceViewResolver do? multithreading
REST (19)
22. What’s internal resource views? Security (1)
Servlet (26)
SOAP (9)
Spring 5 (1)
Popular Tutorials Spring Batch
Spring Batch3
Spring Tutorial Spring Boot
Spring HATEOAS
Spring MVC Web Tutorial
Spring Mobile
Spring Boot Tutorial Spring MVC
Spring Security Tutorial Spring MVC interview
Spring3.0 (90)
Spring AOP Tutorial
Spring4 (12)
Spring JDBC Tutorial SpringSecurity
String in Java
Spring HATEOAS
struts2 (49)
Microservices with Spring Boot thymeleaf (1)
REST Webservice UDDI (1)
WebService
Core Java WSDL (9)
Hibernate Tutorial
Spring Batch
http://www.dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html#5 1/7
2/15/2017 Spring MVC Interview Questions and Answers | Dinesh on Java

7 7 7
1. What is Spring MVC framework?
MENU
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 ▼  2017 (28)
February (15)
of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.
January (13)
Read More about Spring MVC Framework.
►  2016 (60)
►  2015 (11)
2. MVC is an abbreviation for a design pattern. What does it stand for and what is the idea behind it?
►  2014 (89)
MVC is an abbreviation name for a J2EE design pattern and it stands for Model View Controller. According to MVC
►  2013 (352)
design pattern in J2EE application we have three layers of architecture as Controller, Model and Service layers. A
►  2012 (85)
Model View Controller pattern is made up of the following three parts:

ADS
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
http://www.dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html#5 2/7
2/15/2017 Spring MVC Interview Questions and Answers | Dinesh on Java
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

1. <servlet>
2.   <servlet-name>webapp1</servlet-name>
3.     <servlet-class>
4.        org.springframework.web.servlet.DispatcherServlet
5.     </servlet-class>
6.
7.  <!-- We require this configuration when we want to change the default
8.  name / location of the servlet specific configuration files -->
9.  <init-param>
10.    <param-name>contextConfigLocation</param-name>
11.    <param-value>/WEB-INF/app1-servlet.xml</param-value>
12.  </init-param>
13.  <load-on-startup>0</load-on-startup>
14. </servlet>

B. Configure Spring ContexLoaderListner but it is optional

1. <!-- This is the root application context for whole web application. -->
2. <context-param>
3.   <param-name>contextConfigLocation</param-name>
4.   <param-value>/WEB-INF/rootApplicationContext.xml</param-value>
5. </context-param>
6. <listener>
7.   <listener-class>
8.     org.springframework.web.context.ContextLoaderListener
9.   </listener-class>
10. </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.

http://www.dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html#5 3/7
2/15/2017 Spring MVC Interview Questions and Answers | Dinesh on Java

1. @Target(value=PARAMETER)
2. @Retention(value=RUNTIME)
3. @Documented
4. 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.

http://www.dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html#5 4/7
2/15/2017 Spring MVC Interview Questions and Answers | Dinesh on Java
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.

Spring MVC Related Posts


1. Spring MVC Web Tutorial
2. Spring MVC InternalResourceViewResolver
3. MVC Design Pattern
4. Spring MVC DispatcherServlet
5. Spring MVC WebApplicationContext and Root Application Context
6. Spring MVC @Controller Annotation
7. Spring MVC @RequestMapping Annotation
8. Spring MVC @RequestParam Annotation
9. Spring MVC ContextLoaderListener
10. Spring MVC @RequestParam and @PathVariable annotations
11. Spring MVC Hello World Example
12. Spring MVC Exception Handling Example
13. Spring MVC with Hibernate CRUD Example

http://www.dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html#5 5/7
2/15/2017 Spring MVC Interview Questions and Answers | Dinesh on Java
14. Spring MVC Tiles Plugin with Example
15. Spring MVC Interceptor with example
16. Spring MVC with MongoDB CRUD Example
17. Spring MVC Internationalization & Localization with Example

You might also like:

Spring Mobile Spring CRUD hashCode() and Difference Between Spring Boot


Tutorial Example using equals() methods in Merge And Update Tutorial­
Many to One Java Methods In Introduction to
Mapping Hibernate Spring Boot
Linkwithin

+2   Recommend this on Google  

0 Comments dineshonjava 
1 Login

Sort by Best
 Recommend 4 ⤤ Share

Start the discussion…

Be the first to comment.

ALSO ON DINESHONJAVA

Spring MVC Exception Handling Example | DOJ Serialization in Java | DOJ Software Consultant
Software Consultant 1 comment • 2 years ago•
1 comment • a year ago• manish thakur — If a class has a reference of another class, all
karthik veerababu chitrothu — Thanks the references must be Serializable otherwise serialization
process will not be performed. In such case, …

Hadoop Architecture | DOJ Software Consultant Essentials and Key Components of Spring Boot | DOJ
3 comments • 2 years ago• Software Consultant
raja — Hi DineshYou are providing awesome tutorials ,Not able 4 comments • 8 months ago•
to click from module 4 onwards ..links are missing i Dinesh Rajput — Thanks Vijay.
guess.http://www.dineshonjava.com/20...

✉ Subscribe d Add Disqus to your site Add Disqus Add Privacy

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

POPULAR POSTS

Spring 3.0 MVC with Hibernate 3.0 CRUD Example
In this example show how to write a simple web based application with CRUD  operation using Spring3 MVC Framwork with Hibernate3 using Annot...

http://www.dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html#5 6/7
2/15/2017 Spring MVC Interview Questions and Answers | Dinesh on Java
Core JAVA Tutorial ­ Core JAVA a Baby Step to be a Best Java ian
Hello Friends, Now we will focused on the Core Java tutorial, it is really a baby step to be a good, better and best Java ian :­)  . ...

Spring Tutorial ­ A Baby Step to Learn
In this series of spring tutorial, it’s provides many step by step examples and explanations on using Spring framework. The Spring framewo...

Spring Security Tutorial take a Baby step to be Secure
In this spring security tutorial we will discuss about some of the security tips about the Spring Framework. Spring Security is a powerful...

How does java Hashmap work internally
What is Hashing? Hashing in its simplest form, is a way to assigning a unique code for any variable/object after applying any formula/algor...

REST and SOAP Web Service Interview Questions
In this interview questions tutorial we will explain most asking interviews questions on the web services like SOAP, REST etc and its protoc...

NamedParameterJdbcTemplate in Spring with Example
The NamedParameterJdbcTemplate class helps you specify the named parameters inste a d of classic placeholder('?') argument. Named pa...

Hibernate Tutorial ­ Hibernate 3 on Baby Steps
This hibernate tutorial provide step by step instructions on using Hibernate 3.0. Hibernate is popular open source object relational ma...

Spring Batch Tutorial­Spring Batch with Example
Hi In this spring batch tutorial I will discuss about one of the excellent feature of Spring Framework name Spring Batch. Spring Batch is a ...

Spring MVC Tutorial with Examples
Model view controller is a software architecture design pattern.  It provides solution to layer an application by separating three concerns...

http://www.dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html#5 7/7

You might also like