Preparing for Spring Professional Certification. Questions about Spring MVC

Greetings to all.







This is the 5th article in a series of training articles with Spring Professional Certification.







Table of contents
  1. Dependency injection, container, IoC, beans
  2. AOP (aspect-oriented programming)
  3. JDBC, Transactions, JPA, Spring Data
  4. Spring boot
  5. Spring mvc
  6. Spring security
  7. REST
  8. Testing


Let me remind you that these articles are answers to questions from the official guide from Pivotal in preparation for certification.
















MVC is an abbreviation of a special pattern. What is this pattern and what is it for?

MVC is a special template. He divides the program into 3 types of components:







  • Model - the model is responsible for data storage.
  • View - responsible for the output of data on the frontend.
  • Controller - is responsible for exchanging data with view. He operates with models.








What is a DispatcherServlet and where is it used?

This is one of the main parts of MVC for data exchange. This is the main servlet that distributes requests between regular servlets.







  • It receives requests and sends them to registered handlers.
  • Handles views by matching their names with ViewResolver objects
  • Handles thrown exceptions








What is the life cycle of a request?
  1. The request comes in DispatcherServlet
  2. DispatcherServlet sends a request to one of the controllers based on the URL from the request
  3. The controller processes the request, delegates the execution of business logic to the business layer (usually these are classes with the @Service



    annotation), and creates a model with data, which it sends back to DispatcherServlet
  4. DispatcherServlet sends the model to the front for the view, based on the ViewResolver interface (more on this below)


How is the creation of DispatcherServlet done?

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:







  • By defining special properties in web.xml
  • Overriding AbstractAnnotationConfigDispatcherServletInitializer


What is a WebApplicationContext? What additional scopes does he bring?

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


Why do I need session scope?

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.







What is the default scope in MVC?

Just like in Spring without MVC - singleton.







Why use the @Controller annotation?

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.







How do incoming requests map to handler methods?
  1. DispatcherServlet receives a request
  2. DS contains a list of classes that implement the HandlerMapping



    interface HandlerMapping



  3. The handler finds the method there and sends a request to it in the controller class
  4. Handler method executes the request


Tell us about the annotation @RequestMapping

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.







What are annotations @GetMapping, @PostMapping, @DeleteMapping and others?

These are narrower annotations for mapping http methods.







  • @GetMapping



    - Handles get requests
  • @PostMapping



    - Handles post requests
  • @DeleteMapping



    - Handles delete requests
  • @PutMapping



    - Handles put requests
  • @PatchMapping



    - Handles patch requests


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



    - URI
  • headers



    - headers
  • name



    - handler name
  • params



    - parameters
  • produces



    - type of returned data (JSON, XML, text). Used in REST
  • consumes



    - type of data received. Used in REST







    By default, the annotation takes the path to the method.

    @GetMapping("managers") = @GetMapping(path = "managers")











Why use the @RequestParam annotation?

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 }
      
      





Why use the @PathVariable annotation?

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 }
      
      





What parameters can handler methods accept?

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 //      }
      
      





What objects can be used (English)







What other annotations are there for use next to method parameters?
  • @MatrixVariable



    - Indicates that the parameter should be associated with a name-value pair from the URI.
  • @RequestHeader



    - Indicates that the parameter should be associated with the header of the web request.
  • @CookieValue



    - Indicates that the parameter should be associated with cookies.

    The parameter can be declared as a cookie type or as a cookie value type (String, int, etc.).
  • @ModelAttribute



    - Indicates that the parameter is associated with a named model attribute available for view.
  • @SessionAtribute



    - Indicates that the parameter is associated with an attribute from the session.


What is the @RequestBody annotation?

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.







What can a controller method return?
Types of returned objects and their description (English)






What is a View?

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.







How is View selected in the rendering phase? How is View displayed?

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:







  1. The controller returns the name view in the DispactherServlet
  2. Name maps to names in ViewResolver



  3. If a suitable ViewResolver



    is ViewResolver



    , it returns the View that should be used when rendering.
  4. DS transfers the model with the data in the View and displays the output (html page)


What is a Model?

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.








All Articles