Greetings to all.
This is the 5th article in a series of training articles with Spring Professional Certification.
Let me remind you that these articles are answers to questions from the official guide from Pivotal in preparation for certification.
MVC is a special template. He divides the program into 3 types of components:
This is one of the main parts of MVC for data exchange. This is the main servlet that distributes requests between regular servlets.
@Service
DS is created before creating the ApplicationContext.
Starting with Spring 3.2, an implementation of the WebApplicationInitializer interface called AbstractAnnotationConfigDispatcherServletInitialize is used.
AbstractAnnotationConfigDispatcherServletInitializer creates both DispatcherServlet and ContextLoaderListener.
There are two ways to configure DS:
This is a special context for a web application.
It has all the features of a regular ApplicationConext (because it inherits from it), but it also has methods for the standard Servlet API.
Note: Scope is the scope.
Scope | Description |
---|---|
Request
| Scope - 1 HTTP request. A new bean is created for each request. |
Session
| Scope - 1 session. A new bean is created for each session. |
Application
| Scope - ServletContext Life Cycle |
WebSocket
| Scope - WebSocket Life Cycle |
Session scoped bean is a bean that exists while the session exists. It can be used when creating a basket in an online store, etc.
Just like in Spring without MVC - singleton.
The Controller annotation is used to register http request handlers. This is a class-level annotation that contains a Component annotation. The controller class looks like a regular POJO, with handler methods and annotations.
HandlerMapping
HandlerMapping
This annotation is mainly used to specify a URI for a controller class. Previously, it was used by class methods to indicate the URI, http method, type of data sent, etc. In newer versions of Spring, it was replaced with @GetMapping, @PostMapping, etc. annotations. Now it is used only to indicate the URI to the controller class.
These are narrower annotations for mapping http methods.
@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
@PatchMapping
Everything written below is also characteristic of other annotations.
Annotation @GetMapping is simply an annotation that contains @RequestMapping (method = RequestMethod.GET) .
It also allows you to fine-tune the handler method.
Its parameters (they are converted to similar @RequestMapping parameters):
path
headers
name
params
produces
consumes
- type of data received. Used in REST
By default, the annotation takes the path to the method.
@GetMapping("managers") = @GetMapping(path = "managers")
This annotation is used to let handler methods get parameters from an http request.
A request with parameters: http://localhost:8080/getByName/name=Ivan
.
The following code will put the string Ivan
in the variable name
.
@GetMapping("getByName") public User getUserByName(@RequestParam("name") String name) { //some logic }
This annotation gets a specific part from the URI.
URI: http://localhost:8080/getById/23
The following code will put 23
in the id
variable.
@GetMapping("getById/_{id}_") public User getUserById(@PathVariable("id") String id) { //some logic }
Methods in the controller class can use some types of objects as accepted arguments. Then Spring will automatically implement them. For example, the desired HttpSession object, Security, etc.
@GetMapping public User getUserById(HttpSession session) { //some logic // }
@MatrixVariable
@RequestHeader
@CookieValue
@ModelAttribute
@SessionAtribute
It is used to indicate that the method operates not with models, but with data. That is, it sends JSON, XML, text, etc. Usually it is implicitly used in REST services.
View is used to display application data to the user.
Spring MVC supports several View providers (they are called template engines) - JSP, JSF, Thymeleaf, etc.
The View interface transforms objects into regular servlets.
DispatcherServlet contains a list of special "mappers" for view, which, based on the servlet configuration, will contain bins that implement the ViewResolver interface.
View display process:
ViewResolver
ViewResolver
ViewResolver
This is a class object that implements the Model interface and represents a collection of key-value pairs.
The content of the model is used to display data in the View.
For example, if View displays information about the Customer
object, then it can refer to model keys, for example customerName
, customerPhone
, and get values for these keys.
Value objects from the model can also contain business logic.