Q1. What is Spring MVC?
Spring MVC is a web framework built on the Model-View-Controller design pattern.
It provides a clean separation between domain model, web presentation, and request handling.
It's part of the Spring Framework and offers features like declarative request mapping, form handling, validation, and integration with various view technologies (JSP, Thymeleaf, etc.).
It provides a clean separation between domain model, web presentation, and request handling.
It's part of the Spring Framework and offers features like declarative request mapping, form handling, validation, and integration with various view technologies (JSP, Thymeleaf, etc.).
Q2. What is DispatcherServlet?
It intercepts all incoming HTTP requests and delegates them to appropriate handlers (controllers).
It is configured in
It uses
DispatcherServlet is the front controller in Spring MVC.It intercepts all incoming HTTP requests and delegates them to appropriate handlers (controllers).
It is configured in
web.xml or through Java config.It uses
HandlerMapping to determine which controller handles the request, and ViewResolver to resolve views.Q3. What is a Controller in Spring MVC?
A controller is a class that handles HTTP requests. It is annotated with
Methods in the controller are mapped to specific URLs using
Controllers return model and view information, or REST responses.
@Controller.Methods in the controller are mapped to specific URLs using
@RequestMapping (or shortcut annotations like @GetMapping).Controllers return model and view information, or REST responses.
Q4. What is the role of @RequestMapping annotation?
It can be applied at class level (base URL) and method level.
It supports attributes like
Shortcuts:
@RequestMapping maps HTTP requests to handler methods.It can be applied at class level (base URL) and method level.
It supports attributes like
value (URL), method (HTTP method), params, headers, consumes, produces.Shortcuts:
@GetMapping, @PostMapping, etc.Q5. Explain the flow of a Spring MVC request.
1. Client sends request to
2.
3. Controller processes request, sets model data, and returns logical view name.
4.
5. View renders the model and returns response.
DispatcherServlet.2.
DispatcherServlet consults HandlerMapping to find the appropriate controller.3. Controller processes request, sets model data, and returns logical view name.
4.
DispatcherServlet uses ViewResolver to map view name to actual view.5. View renders the model and returns response.
