Problems about Spring MVC

1. What is the process of SpringMVC?

1. The user sends a request to the front controller DispatcherServlet;

2. After DispatcherServlet receives the request, it calls the HandlerMapping processor mapper to request Handle;
3. The processor mapper finds the specific processor according to the request url, generates the processor object and the processor interceptor (if any) and returns it to DispatcherServlet;
4. DispatcherServlet calls the HandlerAdapter processor adapter;
5. HandlerAdapter calls specific handlers (Handler, also called back-end controller) after adaptation;
6. After the Handler is executed, it returns to ModelAndView;
7. HandlerAdapter returns the Handler execution result ModelAndView to DispatcherServlet;
8. DispatcherServlet passes ModelAndView to ViewResolver view resolver for analysis;
9. ViewResolver returns to the specific View after parsing;
10. DispatcherServlet renders the View (that is, fills the model data into the view)
11. DispatcherServlet responds to the user. 

Share a picture

Second, how to lieutenant colonel in Spring MVC Test parameters?

Spring MVC supports JSR-303 verification specification by default. And in Spring-Boot-starter-web provides JSR-303 specification to implement Hibernate Validator. We can use it for parameter verification. For details, please check my related articles.

  

3. What is the use of Spring MVC interceptor and how to use it?

Spring MVC interceptor allows us to intercept the client request and process it in three places-in Before processing, after processing, or after completion (when the view is presented). The interceptor aspect handles some common logic and avoids duplication of processing code (such as logging), and can also be used to change the parameters used globally in the Spring model. In the following way:

org.springframework.web.servlet.handler.HandlerInterceptorAdapter — inherit this class

org.springframework.web.servlet.HandlerInterceptor—implement the interface

  

Four, how to handle controller exception globally?

Through @ControllerAdvice or @RestControllerAdvice and @ExceptionHandler annotation combination, by capturing in method parameters Exceptions are handled, for example as follows:

 
@Slf4j
@RestControllerAdvice("cn.felord.manage.api")
public class GlobalExceptionControllerAdvice {

    @ExceptionHandler(NullPointerException.class)
    public Rest nullPointHandler(HttpServletRequest request, NullPointerException e) {
        log.error("Null pointer, pay attention to the official account: Felordcn", e);
        return RestBody.failure(-1, "null point exception");
    }
}

  

5. How to deal with cross-domain issues in Spring MVC?

Spring MVC has the following methods to solve cross-domain problems:

It is processed by the Spring MVC interceptor, and the filter in the servlet can also be processed in the same way.

By using @CrossOrigin annotations in the control layer method. Please note that this solution needs to be above Spring MVC 4.x.

Through the configuration in the  tag in the Spring MVC xml configuration file.

Configure through `WebMvcConfigurer#addCorsMappings(CorsRegistry)`.

  

VI. Briefly describe the annotation @ModelAttribute.

@ModelAttribute annotation is one of the most important annotations in Spring MVC. It binds method parameters or method return values ​​to the named Model property, and then exposes it to the Web view. If we use it at the method level, it means that the purpose of the method is to add one or more model attributes. On the other hand, when used as a method parameter, it indicates that the parameter should be retrieved from the model. If it does not exist, we should instantiate it first, and then add it to the Model. Once in the model, we should fill in the parameter fields of all request parameters with matching names.

  

7. What are the rules for @Autowired annotations?

@Autowired annotation can be used on member attributes or methods to inject Spring beans by type. This annotation allows Spring to parse the cooperative beans and inject them into the beans that your business needs.

  

8. Why choose Spring MVC?

Spring MVC implements some clear and relatively low coupling concepts, which can make it easy for developers Develop and test their web applications. These concepts are:

Dispatcher Servlet-the core Servlet front controller, configured in the web.xml file.

To intercept matching requests, the Servlet interception matching rules must be defined by yourself, and the intercepted requests are distributed to the target Controller for processing according to the corresponding rules

Controllers-specific business controllers that process specific requested services and respond to them

View Resolvers-view resolvers, used to resolve the response logical view into a real view View object

Views, Models-The main function of Views is to process the response view and then return it to the client. Models are mainly used to transfer control method processing data to the response view page

ModelAndView-a complex of Model and View

Model and Session Attributes-the processing of model attributes and session attributes


These concepts are completely independent and single responsibility. So Spring MVC gives us a lot of flexibility. It is based on interfaces (provided implementation classes), and we can configure each part of the framework with custom interfaces. Another important thing is that we no longer rely on a specific view technology (for example, JSP), we can choose our most composite business view technology. In addition, we not only use Spring MVC in web application development, but also use it to create RESTful web services.

1. The user sends a request to the front controller DispatcherServlet;

2. After DispatcherServlet receives the request, it calls the HandlerMapping processor mapper to request Handle;
3. The processor mapper finds the specific processor according to the request url, generates the processor object and the processor interceptor (if any) and returns it to DispatcherServlet;
4. DispatcherServlet calls the HandlerAdapter processor adapter;
5. HandlerAdapter calls specific handlers (Handler, also called back-end controller) after adaptation;
6. After the Handler is executed, it returns to ModelAndView;
7. HandlerAdapter returns the Handler execution result ModelAndView to DispatcherServlet;
8. DispatcherServlet passes ModelAndView to ViewResolver view resolver for analysis;
9. ViewResolver returns to the specific View after parsing;
10. DispatcherServlet renders the View (that is, fills the model data into the view)
11. DispatcherServlet responds to the user. 

Share pictures

Spring MVC supports JSR-303 verification specification by default. And in Spring-Boot-starter-web provides JSR-303 specification to implement Hibernate Validator. We can use it for parameter verification. For details, please check my related articles.

Spring MVC interceptor allows us to intercept the client request and process it in three places-before processing, after processing Or after completion (while rendering the view). The interceptor aspect handles some common logic and avoids duplication of processing code (such as logging), and can also be used to change the parameters used globally in the Spring model. In the following way:

org.springframework.web.servlet.handler.HandlerInterceptorAdapter — inherit this class

org.springframework.web.servlet.HandlerInterceptor—implement the interface

Through the combination of @ControllerAdvice or @RestControllerAdvice and @ExceptionHandler annotations, handle exceptions by catching exceptions in method input parameters, for example as follows:

 
@Slf4j
@RestControllerAdvice("cn.felord.manage.api")
public class GlobalExceptionControllerAdvice {

    @ExceptionHandler(NullPointerException.class)
    public Rest nullPointHandler(HttpServletRequest request, NullPointerException e) {
        log.error("Null pointer, pay attention to the official account: Felordcn", e);
        return RestBody.failure(-1, "null point exception");
    }
}

Spring MVC has the following methods to solve cross-domain problems:

It is processed by the Spring MVC interceptor, and the filter in the servlet can also be processed in the same way.

By using @CrossOrigin annotations in the control layer method. Please note that this solution needs to be above Spring MVC 4.x.

Through the configuration in the  tag in the Spring MVC xml configuration file.

Configure through `WebMvcConfigurer#addCorsMappings(CorsRegistry)`.

@ModelAttribute annotation is one of the most important annotations in Spring MVC. It binds method parameters or method return values ​​to the named Model property, and then exposes it to the Web view. If we use it at the method level, it means that the purpose of the method is to add one or more model attributes. On the other hand, when used as a method parameter, it indicates that the parameter should be retrieved from the model. If it does not exist, we should instantiate it first, and then add it to the Model. Once in the model, we should fill in the parameter fields of all request parameters with matching names.

@Autowired annotation can be used on member properties or methods to inject Spring beans by type. This annotation allows Spring to parse the cooperative beans and inject them into the beans that your business needs.

Spring MVC implements some clear and relatively low coupling concepts, allowing developers to easily develop and test their Web application. These concepts are:

Dispatcher Servlet-the core Servlet front controller, configured in the web.xml file.

To intercept matching requests, the Servlet interception matching rules must be defined by yourself, and the intercepted requests are distributed to the target Controller for processing according to the corresponding rules

Controllers-specific business controllers that process specific requested services and respond to them

View Resolvers-view resolvers, used to resolve the response logical view into a real view View object

Views, Models-The main function of Views is to process the response view and then return it to the client. Models are mainly used to transfer control method processing data to the response view page

ModelAndView-a complex of Model and View

Model and Session Attributes-the processing of model attributes and session attributes


These concepts are completely independent and single responsibility. So Spring MVC gives us a lot of flexibility. It is based on interfaces (provided implementation classes), and we can configure each part of the framework with custom interfaces. Another important thing is that we no longer rely on a specific view technology (for example, JSP), we can choose the view technology of our most composite business. In addition, we not only use Spring MVC in web application development, but also use it to create RESTful web services.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4739 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.