IBM Global Technology Services
Spring MVC
Simina Muresan
May 2017
© Copyright IBM Corporation 2017
IBM Global Technology Services
Agenda
Spring Framework and what does Spring Offer
What is Spring MVC
Request processing lifecycle
Key Artifacts
Dispatcher Servlet
Controllers
Views
Application
http://start.spring.io/
2 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Spring framework
A popular and stable Java application framework for enterprise development
- Well established in enterprise Java apps
A primary purpose is to reduce dependencies
- Different from almost every other framework out there
- Part of the reason it has been adopted so quickly
Considered an alternative / replacement for the Enterprise JavaBean (EJB) model
What does Spring offer
Dependency Injection
- Also known as IoC (Inversion of Control)
Aspect Oriented Programming
- Runtime injection-based
Portable Service Abstractions
- The rest of spring
ORM, DAO, Web MVC, Web, etc.
3 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Spring framework- Dependency Injection
Any nontrivial application is made up of two or more classes that collaborate with each other to
perform some business logic. Traditionally, each object is responsible for obtaining its own
references to the objects it collaborates with (its dependencies). When applying DI, the objects
are given their dependencies at creation time by some external entity that coordinates each
object in the system. In other words, dependencies are injected into objects.
4 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
What is Spring MVC
Web framework based on Model/View/Controller pattern
Based on “Open for extension, close for modification” principle
5 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Request Processing Lifecycle
6 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
DispatcherServlet – The heart of Spring MVC
A “front controller”
- Coordinates all request handling activities
Delegates to Web infrastructure beans
Invokes user Web components
Fully customizable
- Interface for all infrastructure beans
- Many extension points
An application can have multiple instance of DispatcherServlet each mapped on
a specific URL
7 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
DispatcherServlet– Java Configuration Example ( works only in Servlet 3.0 )
8 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Register Servlets, Filters, Listeners
9 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Web Initializer Classes
10 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
DispatcherServlet – XML Configuration Example (pre Servlet 3.0)
11 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Servlet Container
12 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Controllers @Controller annotation
What is an Controller?
- Controllers interpret user input and transform it into a model that is represented to the user by the
view
@Controller annotation:
- Acts as a stereotype annotations indicating the role
- @ComponentScan("accounts.web") or <context:component-scan base
package="accounts.web"/>
13 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Controllers
Mapping Requests with @RequestMapping annotation
- @RequestMapping(path="/accounts", method=GET) Or @GetMapping(“/accounts”)
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
- UriTemplate Template Patterns -> @PathVariable
- Consumable Media Types -> @RequestBody
- Producible Media Types -> @ResponseBody
- Extracting Request Parameter with @RequestParam
14 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Views and View Resolvers
A view renders web output
Controllers typically return a ‘logical view name’ String
ViewResolvers selects View based on view name
The DispatcherServlet delegates to a ViewResolver to obtain View implementation
based on view name
- InternalResourceViewResolver
- TilesViewResolver
- VelocityViewResolver
- FreeMakerViewResolver
15 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Views and View Resolvers configuring ViewResolver
16 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Application
17 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
start.spring.io
18 28/03/2023 © Copyright IBM Corporation 2017
IBM Global Technology Services
Q&A
19 28/03/2023 © Copyright IBM Corporation 2017