0% found this document useful (0 votes)
57 views5 pages

Spring MVC

Notes on spring MVC

Uploaded by

obeymyshinnyrod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views5 pages

Spring MVC

Notes on spring MVC

Uploaded by

obeymyshinnyrod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Spring MVC

Created @February 5, 2025 11:49 PM

Status Open

Updated @February 6, 2025 12:03 AM

Spring MVC

A web framework based on the Model-View-Controller (MVC) pattern.

It helps in separating concerns:

Model → Represents data and business logic.

View → Handles UI representation.

Controller → Manages user requests and responses.

It is part of Spring Web Module and built on Servlet API.

Model - A model contains the data of the application. A 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.

Spring MVC 1
View - A view represents the provided information in a particular format.
Generally, JSP+JSTL is used to create a view page. Although spring also
supports other view technologies such as Apache Velocity, Thymeleaf and
FreeMarker.

Front Controller - In Spring Web


MVC, the DispatcherServlet class works as the front controller. It is
responsible to manage the flow of the Spring MVC application.

Spring MVC Architecture


Spring MVC follows a request-driven architecture where each request is
processed as follows:

1. Client sends a request (e.g., via browser).

2. DispatcherServlet intercepts the request.

3. Handler Mapping finds the appropriate controller.

4. Controller processes the request and returns a ModelAndView object.

5. View Resolver finds the correct view (e.g., JSP, Thymeleaf).

6. Response is generated and sent back to the client.

Key Components of Spring MVC

Component Description
DispatcherServlet Front Controller that handles all HTTP requests.
Controller Handles user input and calls services.
Model Holds application data.
View Defines the UI (JSP, Thymeleaf, etc.).
ViewResolver Maps logical view names to actual views.
HandlerMapping Finds the right controller for the request.

Spring MVC 2
Flow

Client → DispatcherServlet → HandlerMapping → Controller → Service → Mod


el → ViewResolver → View → Client

1. First received by dispatcherserverlt

2. DS will take help of handler mapping and it will get to know the specific
controller

3. Controller will process this request by executing appropriate methods

4. Transfer this object to view resolver to get the actual view page

Spring MVC 3
5. DS passes the model object to view page which display the result

Annotations in Spring MVC


Annotation Description
@Controller Marks a class as a Spring MVC controller.
@RestController Same as @Controller but returns JSON/XML instead of a view.
@RequestMapping Maps HTTP requests to handler methods.

@GetMapping ,
Shortcuts for specific HTTP methods.
@PostMapping

@RequestParam Extracts query parameters from URL.


@PathVariable Extracts values from URL path.
@ModelAttribute Binds form data to a model object.

@Controller
public class UserController {

@GetMapping("/user")
public String getUser(@RequestParam String name, Model model) {
model.addAttribute("username", name);
return "user";
}

@GetMapping("/user/{id}")
public String getUserById(@PathVariable int id, Model model) {
model.addAttribute("userId", id);
return "user";
}
}

ViewResolvers in Spring MVC

Spring MVC 4
Spring MVC supports different View Resolvers to map logical view names to
actual views.

View Resolver Description


InternalResourceViewResolver Maps to JSP pages.
ThymeleafViewResolver Maps to Thymeleaf templates.
MappingJackson2JsonView Converts response to JSON (for REST APIs).

Key Points to Know


✔ Spring MVC follows the Model-View-Controller pattern.
✔ is the Front Controller handling all requests.
DispatcherServlet

✔ Use for handling web requests,


@Controller for APIs.
@RestController

✔ Use for rendering JSP, Thymeleaf, or JSON.


ViewResolvers

✔ Spring Boot makes Spring MVC configuration easier (No XML needed).
✔ Spring MVC allows form handling using .
@ModelAttribute

✔ Exception handling is done using and


@ExceptionHandler . @ControllerAdvice

Spring MVC 5

You might also like