Spring MVC
Spring MVC
DEVELOPMENT PROGRAM
Spring MVC
OUTLINE
• DispatcherServlet
• Controller
MVC
• MVC is an architecture that separates business logic, presentation and data.
In MVC,
• MVC is a systematic way to use the application where the flow starts from
the view layer, where the request is raised and processed in controller layer
and sent to model layer to insert data and get back the success or failure
message.
MVC
Why MVC
• Separation of Concern
• Loose coupling
between model, view,
and controller
• The DispatcherServlet, as any Servlet, needs to be declared and mapped according to the
Servlet specification by using Java configuration or in web.xml. In turn,
the DispatcherServlet uses Spring configuration to discover the delegate components it needs
for request mapping, view resolution, exception handling.
• Parsing of HTTP request data and headers into data transfer objects (DTOs) or
domain objects
• Model-view-controller interaction
• init-
It injects the bean’s properties using the servlet
param values received from the web.xml or
from WebApplicationInitializer
• The HttpServlet.service() implementation, which routes requests by the type of HTTP verb,
makes perfect sense in the context of low-level servlets
• However, at the Spring MVC level of abstraction, method type is just one of the parameters that can be used to
map the request to its handler
• In J2EE servlet, we have to map the controller / servlet to its own url manually. However, DispatchServlet
• WebApplicationContext — configuration
• This may be very useful during redirects (like showing the user a one-
shot information message after the redirect)
DISPATCHING THE REQUEST
• After the enriching process, DispatchServlet will call dispatch() method
• The main purpose of the dispatch() method is to find an appropriate handler for the request and feed it
the request/response parameters
• To find the handler that matches the request, Spring goes through the registered implementations of
the HandlerMapping interface
• Note: the HandlerAdapter won’t render the view, it just return the
object to the DispatchServlet
PROCESSING ARGUMENTS
AND RETURN VALUES
• Now it is the time to handle the request and process our business logic.
• Note that you are not required to return a ModelAndView instance from a controller method.
You may return a view name, or a ResponseEntity or a POJO that will be converted to a JSON
response.
• In the ModelAndView object, we will specify the view name, such as login.jsp, so
Spring has to look it up. This is where the ViewResolvers list comes into play
• Then Spring will call the view’s render() method — Spring finally completes the
request processing by sending the HTML page to the user’s browser
CONTROLLER
• Spring MVC provides an annotation-based programming model
where @Controller and @RestController components use annotations
to express request mappings, request input, exception handling
REQUEST MAPPING
• The @RequestMapping annotation is used to map requests to controllers
methods. It has various attributes to match by URL, HTTP method, request
parameters, headers, and media types
• It can be used at the class level to express shared mappings or at the method level to
narrow down to a specific endpoint mapping
REQUEST MAPPING
• There are also HTTP method specific shortcut variants
of @RequestMapping
• @GetMapping
• @Postmaping
• @PutMapping
• @DeleteMapping
• @PatchMapping
REQUEST MAPPING
• Path Variable
• Simple types (int, long, Date, and so on) are supported by default and you can register
support for any other data type.
REQUEST MAPPING
• Request Parameter
• Spring MVC provides support for customizing the URL in order to get
data. To achieving this purpose @PathVariable annotation is used in
Spring mvc framework.
REQUEST MAPPING
• @RequestHeader annotation is used to bind a request
header to a method argument in a controller
REQUEST MAPPING
• The @RequestBody annotation is used to have
the request body read and deserialized into
an Object
REQUEST MAPPING
• The @ResponseBody annotation is used on a
method to have the return serialized to the
response body.
ANY QUESTIONS?