- @ControllerAdvice
- @ExceptionHandler 捕捉Controller中发生的异常
- @InitBinder 设置WebDataBinder,即设置请求数据中的统一操作,过滤,添加等等
- @ModelAttribute 绑定键值对到Model中,在Spring中,所有的请求都会有一个Model
@ControllerAdvice
public class ExceptionHandlerAdvice {
@ExceptionHandler(value = Exception.class)
public ModelAndView exception(Exception exception, WebRequest request) {
ModelAndView modelAndView =
new ModelAndView("error");
modelAndView.addObject("errorMessage", exception.getMessage());
return modelAndView;
}
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.setDisallowedFields("time");
}
@ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("time", new Date().getTime());
}
}