Spring MVC Interview Questions 2020
A list of top frequently asked Spring MVC Interview Questions and answers are given below.
1) What is MVC?
The MVC (Model-View-Controller) is a software architectural design pattern. It separates the functionality of an application into three interconnected parts - Model, View, and Controller. This approach facilitates the reusability of the code and parallel development needs Spring Boot Online Training
2) What is Spring MVC?
A Spring MVC is a Java Framework which is used to develop dynamic web applications. It implements all the basic features of a core spring framework like Inversion of Control and Dependency Injection. It follows the Model-View-Controller design pattern.
Here,
• Model - A model contains the data of the application. Data can be a single object or a collection of objects.
• Controller - A controller contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller.
• View - A view represents the provided information in a particular format. So, we can create a view page by using view technologies like JSP+JSTL, Apache Velocity, Thymeleaf, and FreeMarker.
3) What is the front controller of Spring MVC?
The front controller is a DispatcherServlet class present in org.springframework.web.servlet package. It dispatches the request to the appropriate controller and manages the flow of the application. It is required to specify the DispatcherServlet class in the web.xml file.
4) Explain the flow of Spring MVC?
• Once the request has been generated, it intercepted by the DispatcherServlet that works as the front controller.
• The DispatcherServlet gets an entry of handler mapping from the XML file and forwards the request to the controller.
• The controller returns an object of ModelAndView.
• The DispatcherServlet checks the entry of view resolver in the XML file and invokes the specified view component.
5) What are the advantages of Spring MVC Framework?
The following are the advantages of Spring MVC Framework : -
• Separate roles - The Spring MVC separates the application into three interconnected layers where each layer has its role.
• Light-weight - It uses light-weight servlet container to develop and deploy your application.
• Powerful Configuration - It provides a robust configuration for both framework and application classes that includes easy referencing across contexts, such as from web controllers to business objects and validators.
• Rapid development - The Spring MVC facilitates fast and parallel development.
• Reusable business code - Instead of creating new objects, it allows us to use the existing business objects.
• Flexible Mapping - It provides the specific annotations that easily redirect the page.
6) What does an additional configuration file contain in Spring MVC application?
The Spring MVC application contains an additional configuration file that contains the properties information. This file can be created either in the form of an xml file or properties file. In this file, we generally define the base-package and view resolver where DispatcherServlet searches for the controller classes and view components path. However, it can also contain various other configuration properties.
7) What is an InternalResourceViewResolver in Spring MVC?
The InternalResourceViewResolver is a class which is used to resolve internal view in Spring MVC. Here, you can define the properties like prefix and suffix where prefix contains the location of view page and suffix contains the extension of view page. For example:-
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
8) How to declare a class as a controller class in Spring MVC?
The @Controller annotation is used to declare a class as a controller class. It is required to specify this annotation on the class name. For example:-
@Controller
class Demo
{
}
9) How to map controller class and its methods with URL?
The @RequestMapping annotation is used to map the controller class and its methods. You can specify this annotation on the class name as well as method name with a particular URL that represents the path of the requested page. For example:-
@Controller
@RequestMapping("/ form")
class Demo
{
@RequestMapping("/show")
public String display()
{
}
}
10) Name the annotations used to handle different types of incoming HTTP request methods?
The following annotations are used to handle different types of incoming HTTP request methods: -
• @GetMapping
• @PostMapping
• @PutMapping
• @PatchMapping
• @DeleteMapping
More Interview Questions On Spring Boot Interview Questions