Questions about a Spring MVC

First, what SpringMVC is the process?

1. The client sends a request to the front end of the controller the DispatcherServlet; 

2. the DispatcherServlet after receiving the request, the call processor HandlerMapping mapper request to obtain the Handle; 
3. mapper processor to locate specific processor upon request url, objects, and generation processor processor blocker (if any) collectively returned to the DispatcherServlet; 
4. HandlerAdapter the DispatcherServlet call processor adapter; 
5. The specific call processor adapted HandlerAdapter through (Handler, also called back-end); 
6. The Handler returns execution to complete ModelAndView; 
7. the Handler HandlerAdapter is the result of execution returns to the ModelAndView the DispatcherServlet; 
8. the ModelAndView will pass the DispatcherServlet parser parses ViewResolver view; 
return after DETAILED view ViewResolver 9. the parsed; 
10. the view render the view of the DispatcherServlet (filling about to model data to view) 
11. the response to the DispatcherServlet user.

 

 

 

 

 

 

 

Second, how to check parameters in Spring MVC?

Spring MVC default support JSR-303 specification verification. And it provides a JSR-303 specification in the Spring-Boot-starter-web implemented Hibernate Validator. We can use it to check the parameters. For details, see my related article. 

  

Three, Spring MVC interceptors how to use what's the use?

Spring MVC interceptors allow us to intercept the client request and process it in three places - before treatment, after treatment or after completion (when rendering view). Some public interceptor logic processing section to avoid processing the program code (such as logging), can also be used to change parameters in the model Spring globally available. In the following ways: 

org.springframework.web.servlet.handler.HandlerInterceptorAdapter - extending this class 

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

  

Fourth, how to deal with the global controller abnormal? 

@ControllerAdvice or by a combination of notes and @RestControllerAdvice @ExceptionHandler, the process parameters by capturing exception processing, for example as follows: 

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

    @ExceptionHandler (a NullPointerException. class) 
    public Rest nullPointHandler (the HttpServletRequest Request, a NullPointerException E) { 
        log.error ( "null pointer matter, quickly public attention number: Felordcn", E); 
        return RestBody.failure (-1, "null Point Exception"); 
    } 
}

  

5, how to deal with cross-domain problems in Spring MVC?

Spring MVC to solve cross-domain problems are mainly the following ways: 

be handled by Spring MVC interceptors, empathy servlet in the filter can handle. 

By using the method @CrossOrigin annotation control layer. Please note that the program needs in Spring MVC 4.x or higher. 

By Spring MVC xml configuration file <mvc: cors> tag configuration. 

`WebMvcConfigurer # addCorsMappings (CorsRegistry)` configured by.

  

Sixth, briefly comment @ModelAttribute.

@ModelAttribute Spring MVC annotation is one of the most important notes. The method will return a value parameter or method to bind to the Model name attribute, and then exposed to the Web view. If we use it in the method level, it indicates that the purpose of this method is to add one or more model attributes. On the other hand, when used as a method parameter, which represents the model should search parameters. If not, we must first instantiate it, then add it to the Model. Once in the model, we should fill in all the fields with the parameters of the request parameter matching names. 

  

Seven, what @ Autowired annotation rules?

Annotations can be used on @Autowired member attribute or a method, according to the type of injection Spring bean. This annotation allows Spring to resolve collaborative bean and bean injected into the needs of your business. 

  

Eight Why Spring MVC?

Spring MVC implemented some concepts clear and relatively low coupling, it allows developers to easily develop and test their Web applications. These concepts have: 

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

Matching the request interceptor, Servlet matching rule to intercept their own definition, the intercepted request according to the distribution rule corresponding to the target processed Controller 

Controllers - specific service controller, and the processing in response to specific requests service 

View Resolvers - View a parser for parsing the logical view in response to a real view of the view object 

views, Models --Views main role is to handle the response to the view, and then returned to the client, primarily for passing control Models method for processing data in response to view page 

ModelAndView --Model and view complex 

model and session attributes - process model attributes and session attributes of 


these concepts are totally independent and single responsibility. So Spring MVC gives us great flexibility. It is based on the interface (provided by the implementation class), we can use a custom interface configuration portion of each frame. Another important thing is that we are no longer tied to a particular view technology (for example, JSP), we can choose to view the most complex business technology. In addition, we only use Spring MVC in Web application development, you can also use it to create RESTful Web services.

  

 

Guess you like

Origin www.cnblogs.com/pxblog/p/11605238.html