MENU

SpringMVC的@ControllerAdvice注解

概述

@ControllerAdvice
刚接触SpringMVC应该很少会见到这个注解,其实它的作用非常大。
这里来简单介绍一下,如果需要更详细的介绍,建议查询springmvc官方文档,里面的介绍非常详细。
@ControllerAdvice是SpringMVC从3.2开始提供的注解

org.springframework.web.bind.annotation.ControllerAdvice

用它标注的Class能被自动扫描到,主要是用来辅助Controller的
ControllerAdvice主要作用如下:

全局异常处理

定义一个类,用ControllerAdvice注解

// 全局异常处理
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public String myHandler(Model model,Exception e) {
        model.addAttribute("message", e.getMessage());
        return "errorPage";
    }
}

在自定义的类中可以定义多个方法,不同方法处理不同异常,也可以用一个方法处理所有异常。
@ExceptionHandler注解用于指定要处理的异常类型。

全局数据绑定

@ControllerAdvice
public class GlobalExceptionHandler {
    //定义全局数据
    @ModelAttribute(name="globalData")
    public String setGlobalData(){
        return "globalDataString";
    }
}

这样在任意一个Controller里面就都可以获取到该变量了

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(ModelMap modelMap) throws Exception {
        System.out.println(modelMap.get("globalData"));
        return "hello";
    }
}

注:@ModelAttribute
使用该注解标记的方法返回的数据就是一个全局数据。默认情况下key是返回的变量名,value是方法的返回值。我们可以用注解的name属性指定一个key。

附 官方文档相关内容

Spring Framework 5.2.5 Release Docs

1.1.7 Table2 Line5

HandlerExceptionResolverDescription
ExceptionHandlerExceptionResolverResolves exceptions by invoking an @ExceptionHandler method in a @Controller or a @ControllerAdvice class. See @ExceptionHandler methods.

1.3.5 DataBinder

@Controller or @ControllerAdvice classes can have @InitBinder methods that initialize instances of WebDataBinder, and those, in turn, can:

  • Bind request parameters (that is, form or query data) to a model object.
  • Convert String-based request values (such as request parameters, path variables, headers, cookies, and others) to the target type of controller method arguments.
  • Format model object values as String values when rendering HTML forms.

1.3.6 Exceptions

@Controller and @ControllerAdvice classes can have @ExceptionHandler methods to handle exceptions from controller methods

1.3.7 ControllerAdvice

Typically @ExceptionHandler, @InitBinder, and @ModelAttribute methods apply within the @Controller class (or class hierarchy) in which they are declared. If you want such methods to apply more globally (across controllers), you can declare them in a class annotated with @ControllerAdvice or @RestControllerAdvice.

1.4.5 Filtering Handler Functions

You can filter handler functions by using the before, after, or filter methods on the routing function builder. With annotations, you can achieve similar functionality by using @ControllerAdvice, a ServletFilter, or both. The filter will apply to all routes that are built by the builder. This means that filters defined in nested routes do not apply to "top-level" routes.

小结

文档是个好东西,就是看起来头有点大


文章标题:SpringMVC的@ControllerAdvice注解
如果文中内容侵犯了您的权益,请及时与博主取得联系进行删除!
本站文章未经许可禁止转载,本文地址:https://blog.wanvale.com/archives/97/

Last Modified: June 20, 2020
Archives QR Code Tip
QR Code for this page
Tipping QR Code