Familiar usage

The @Controlleradivce annotation is used in conjunction with the @ExceptionHandler annotation to handle exceptions uniformly

InitBinder is used together

The @controlleradivce annotation is used in conjunction with the @initBinder annotation

@ControllerAdvice
public class SpringControllerAdvice {
    @InitBinder
    public void globalInitBinder(WebDataBinder binder) {
        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
  }
}
Copy the code

ModelAttribute is used in combination

@ControllerAdvice public class SpringControllerAdvice { @ModelAttribute(value = "message") public String GlobalModelAttribute () {system.out.println (" Message global attribute added." ); Return "outputs the message global property." ; } } @Controller public class UserController { @RequestMapping(value = "/users", method = RequestMethod.GET) public void users(@ModelAttribute("message") String message) { System.out.println(message); }}Copy the code

conclusion

Methods annotated by @ExceptionHandler, @initBinder, and @ModelAttribute all apply to methods annotated by @requestmappping

Reference article:

www.cnblogs.com/yanggb/p/10…