Top 20 Spring MVC Interview Questions | Spring MVC Interview Questions

Below are the Top 20 Spring MVC Interview Questions based on the candidate's experiences and the company's interview pattern.


 

 

1. What is Spring MVC?

A Spring MVC is a Java Framework that 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.

 

1) Model - A model contains the data of the application. Data can be a single object or a collection of objects.

2) Controller - A controller contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller.

3) 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.

 

2. What is the front controller in 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.

 

3. What are the advantages of the Spring MVC Framework?

The following are the advantages of the Spring MVC Framework: -

1) Separate roles - The Spring MVC separates the application into three interconnected layers where each layer has its role.

2) Light-weight - It uses a lightweight servlet container to develop and deploy your application.

3) Powerful Configuration - It provides a robust configuration for both framework and application classes that includes easy referencing across contexts, such 4) as from web controllers to business objects and validators.

5) Rapid development - The Spring MVC facilitates fast and parallel development.

6) Reusable business code - Instead of creating new objects, it allows us to use the existing business objects.

7) Flexible Mapping - It provides specific annotations that easily redirect the page.

 

4. What is an InternalResourceViewResolver in Spring MVC?

The InternalResourceViewResolver is a class that 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>

 

5. 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 TestController  

{  

  

}   

 

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

 

1) @GetMapping

2) @PostMapping

3) @PutMapping

4) @PatchMapping

5) @DeleteMapping

 

7. What do you mean by ModelAndView in Spring MVC?

The ModelAndView is a class that holds both Model and View where the model represents the data, and view represents the representation of that data. This class returns the model and view in the single return value.

 

8. What is ModelMap in Spring MVC?

The ModelMap is a class that provides the implementation of Map. It extends the LinkedHashMap class. It facilitates passing a collection of values as if they were within a Map.

 

9. What is the Spring MVC form tag library?

The Spring MVC form tags can be seen as data binding-aware tags that can automatically set data to Java object/bean and also retrieve from it. These tags are the configurable and reusable building blocks for a web page. It provides view technologies, an easy way to develop, read, and maintain the data.

 

10. What do you understand by validations in Spring MVC?

The validation is one of the most important features of Spring MVC, that is used to restrict the input provided by the user. To validate the user's input, it is required to use the Spring 4 or higher version and Bean Validation API. Spring validations can validate both server-side as well as client-side applications.

 

11. What is the use of @Valid annotation in Spring MVC?

The @Valid annotation is used to apply validation rules on the provided object.

 

12. How to validate user's input within a number range in Spring MVC?

In Spring MVC Validation, we can validate the user's input within a number range by using the following annotations: -

 

1) @Min annotation - It is required to pass an integer value with @Min annotation. The user input must be equal to or greater than this value.

2) @Max annotation - It is required to pass an integer value with @Max annotation. The user input must be equal to or smaller than this value.

 

13. What is the purpose of custom validations in Spring MVC?

The Spring MVC framework allows us to perform custom validations. In such a case, we declare our own annotations. We can perform validation based on own business logic.

 

14. What is the ContextLoaderListener and what does it do?

The ContextLoaderListener is a listener which helps to bootstrap Spring MVC. As the name suggests it loads and creates ApplicationContext, so you don't have to write explicit code to do create it.

The application context is where Spring bean leaves. For a Web application, there is a subclass called WebAppliationContext.

The ContextLoaderListener also ties the lifecycle of the ApplicationContext to the lifecycle of the ServletContext. You can get the ServletContext from WebApplicationContext using getServletContext() method.

 

15. What is the @RequestParam used for?

The @RequestParam is a Spring MVC annotation that is used to extract request parameter or query parameters from URL in Controller's handler method as shown below:

public String personDetail(@RequestParam("id") long id){

  ....

  return "employeeData";

}

 

16. What is the purpose of the session scope?

The purpose of the session scope is to create an instance of the bean for an HTTP Session. This means the same bean can serve multiple requests if it is scoped in session. You can define the scope of a Spring bean using the scope attribute or @Scope annotation in the Spring MVC application.

 

17. What is the default scope in the web context?

The singleton scope is the default scope for a Spring bean even in the web context. The other three Web context-aware scopes are a request, session, and global-session, which are only available in a web application-aware ApplicationContext object.

 

18. Why are controllers testable artifacts? 

In Spring MVC, Controllers are testable artifacts because they are not directly coupled with any View technology. They just return a logical view name, which can be easily tested.

 

19. What do you understand by Spring MVC Tiles?

The Spring provides integration support with the apache tiles framework. So we can manage the layout of the Spring MVC application with the help of spring tiles support. The following are the advantages of Tiles support in Spring MVC: -

 

1) Reusability: We can reuse a single component in multiple pages like header and footer components.

2) Centralized control: We can control the layout of the page by a single template page only.

3) Easy to change the layout: With the help of a single template page, we can change the layout of the page anytime. So your website can easily adopt new technologies such as bootstrap and jQuery.

 

20. Is the DispatcherServlet instantiated via an application context?

No, DispatcherServlet is instantiated by Servlet containers like Tomcat or Jetty. You must define DispatcherServlet into the web.xml file.

You can see that the load-on-startup tag is 1 which means DispatcherServlet is instantiated when you deploy the Spring MVC application to Tomcat or any other servlet container. During instantiation, it looks for a file servlet-name-context.xml and then initializes beans defined in this file.