Spring MVC Interview Questions and Answers

This section covers frequently asked Spring MVC interview questions.

1. What is MVC (Model-View-Controller)?

MVC is a software design pattern separating an application into three interconnected parts:

  • Model: Manages data and business logic.
  • View: Displays the data to the user.
  • Controller: Handles user input and updates the model.

2. What is Spring MVC?

Spring MVC is a Java framework implementing the MVC pattern. It's built on the Spring Framework, leveraging features like Inversion of Control (IoC) and Dependency Injection (DI) to create flexible and maintainable web applications.

3. Front Controller in Spring MVC.

DispatcherServlet acts as the front controller in Spring MVC. It intercepts all incoming requests and routes them to the appropriate controllers.

4. Spring MVC Workflow.

  1. A request is received by DispatcherServlet.
  2. DispatcherServlet finds a suitable controller based on the request URL.
  3. The controller processes the request and returns a ModelAndView object.
  4. DispatcherServlet uses a ViewResolver to determine the appropriate view (e.g., JSP) to render.
  5. The view renders the data provided by the model.

5. Advantages of Spring MVC.

  • Separation of concerns (Model, View, Controller).
  • Lightweight design.
  • Powerful configuration options.
  • Supports rapid development.
  • Reusability of business logic.
  • Flexible URL mapping.

6. Spring MVC Configuration File.

A configuration file (XML or properties) specifies settings like the base package for controllers and view resolvers. This file is usually located in the application's configuration directory.

7. InternalResourceViewResolver.

InternalResourceViewResolver resolves views based on a prefix and suffix. This helps keep view locations consistent.

8. Declaring a Controller Class.

Use the @Controller annotation.

Example

@Controller
public class MyController {
  // ...
}

9. Mapping Controllers and Methods to URLs.

Use the @RequestMapping annotation.

Example

@Controller
@RequestMapping("/users")
public class UserController {
  @RequestMapping("/list")
  public String listUsers() {
    // ...
  }
}

10. Annotations for HTTP Request Methods.

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @PatchMapping
  • @DeleteMapping

11. @PathVariable Annotation.

@PathVariable extracts values from URI path variables.

Example

@GetMapping("/users/{id}")
public String getUser(@PathVariable("id") int userId) {
  // ...
}

12. @ResponseBody Annotation.

@ResponseBody automatically serializes the method's return value (often as JSON) into the HTTP response body.

13. The Model Interface.

The Model interface represents the data to be passed to the view.

14. ModelAndView.

ModelAndView holds both the model data and the view name.

15. ModelMap.

ModelMap is a Map implementation used to add data to the model.

16. Reading Form Data in Spring MVC.

  • HttpServletRequest
  • @RequestParam annotation
  • @ModelAttribute annotation

17. Spring MVC Form Tag Library.

Spring's form tag library provides data-binding-aware tags for creating HTML forms.

18. Validations in Spring MVC.

Spring MVC supports data validation, often using the Bean Validation API (like Hibernate Validator).

19. Bean Validation API.

A Java specification for declaring validation constraints using annotations.

20. @Valid Annotation.

@Valid triggers validation on an object.

21. BindingResult.

BindingResult holds validation results.

22. Validating Number Ranges.

  • @Min
  • @Max

23. Validating Input Sequences.

Use the @Pattern annotation with a regular expression.

24. Custom Validations.

Create custom annotations to implement application-specific validation rules.

25. Spring MVC Tiles.

Spring MVC integrates with Apache Tiles for defining reusable view components (like headers and footers).

Java-Related Interview Questions:

  • Java Basics: Fundamental concepts of Java programming.
  • Java OOPs (Object-Oriented Programming): Core OOP principles like inheritance, polymorphism, encapsulation, and abstraction.
  • Java Multithreading: Concepts like threads, synchronization, and concurrency.
  • Java String and Exception Handling: Working with strings and exception handling mechanisms.
  • Java Collections: Understanding and using various Java collection types (Lists, Sets, Maps, etc.).
  • JDBC (Java Database Connectivity): Connecting and interacting with databases from Java.
  • Servlets: Developing server-side components for web applications.
  • JSP (JavaServer Pages): Creating dynamic web pages.
  • Spring Framework: A popular Java framework for building enterprise applications.
  • Hibernate: An ORM (Object-Relational Mapping) framework for Java.

Database-Related Interview Questions:

  • PL/SQL (Procedural Language/SQL): Procedural extensions to SQL, often used with Oracle databases.
  • SQL (Structured Query Language): The standard language for managing and querying relational databases.
  • Oracle Database: A widely used relational database management system (RDBMS).
  • SQL Server: Microsoft's RDBMS.
  • MySQL: Another popular open-source RDBMS.