A few minutes will take you to quickly understand the theoretical knowledge of the SpringMVC framework!

1. Understanding of Spring MVC

MVC is actually the abbreviation of Model View Controller. It is a pattern framework that separates presentation and interaction. It is divided into model, view and controller.

(1) Model model: the main part of the program, mainly including business data and business logic. At the model layer, it also involves services published by users, in which the data in the business model will be updated according to different business needs.

(2) View view: The part that the program presents to the user is the entry point for the interaction between the user and the program. The user inputs the required business data through the view, and through the event interaction of the interface, submits the corresponding input parameters to the background manager for processing.

(3) Controller controller: used to process the data input by the user and update the part of the business model. The controller receives the data passed when the user interacts with the interface, and executes the service call and updates the data and status of the business model according to the data business logic.

2. The execution process of SpringMVC

(1) The user sends a request through the client or browser

(2) DispatcherServlet calls the processor mapper HandlerMapping to resolve the corresponding Handler according to the request information

(3) In fact, the parsed Handler is the Controller controller

(4) The processor adapter HandlerAdapter will find the real processor according to the Handler to process the request and execute the corresponding business logic

(5) After the processor finishes processing, it will return a ModelAndView, the Model is the returned data, and the View is the logical view

(6) The view resolver ViewResolver will resolve the View logical view into a physical view

(7) DispatcherServlet will pass it to View according to the returned Model

(8) Return the View to the requester

Notice:

(1) DispatcherServlet is the front controller: it is the core component inside, the entry function of SpringMVC, the interface request, and the corresponding result. The front-end controller is similar to a central controller, and its existence reduces the coupling between various components. After the request to the controller, it is the controller inside, which is responsible for calling other components to process user requests, and is also the center of the entire process control.

(2) HandlerMapping is a processor mapper: find Handler according to user request

(3) HandlerAdapte is an adapter processor: execute Handler according to specific rules

(4) ViewResolver is a view resolver: to resolve the returned logical view into a physical view is to find the physical path, and finally render the View to the user

(5) Handler is a processor

(6) View is a view

3. Settings for SpringMVC forwarding and redirection

(1) Forward: add forward in front of the return value

(2) Redirection: add redirect before the return value

4. What are the annotations commonly used in SpringMVC

(1) @ReuqestMapping: It can be placed on the class or on the method. If it is placed on the method, it is the request path of the method. If it is on the class, all methods under this class must be preceded by the parent path. .

(2) @RequestBody: When requesting, json can be transmitted as a parameter to the method and converted into an object.

As the following code snippet:

When you want to access the getUserAuth method, the path is the prefix /system/getUserAuth. The /system inside is the parent path, and /getUserAuth and /captcha are the access paths on each method. Since @RequestMapping is defined on the class, each method access needs to add the parent path.

@Controller
@RequestMapping("/system")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysCaptchaService sysCaptchaService;

    @RequestMapping("/getUserAuth")
    public LoginVo getUserAuth(@RequestParam("username") String username) throws Exception {
        return sysUserService.getUserAuth(username);
    }

    /**
     * verification code
     */
    @RequestMapping("/captcha")
    public void captcha(HttpServletResponse response, String uuid)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }


}

(3) @ResponseBody: Convert the return object of the control layer method into a json response to the front-end client.

5. SpringMVC handles unified exceptions

method one:

Customize an exception handling class to implement the HandlerExceptionResolver interface, and implement the exception handling method inside, and then hand this class over to the Spring container for management.

Method 2:

Add the @ControllerAdvice table name to the class as a global exception handling class, and add the annotation @ExceptionHandler to the method. In this annotation, there is a value attribute value that can specify the type that can be handled.

Guess you like

Origin blog.csdn.net/lf21qp/article/details/131457063