@controllerAdvice:我们可以将对于使用@Controller注解的控制器的全局配置放在同一个位置。
对于注解了@Controller的类,该类下可以使用@ExceptionHandler、@InitBinder、@modelAttribute。@RequestMapping的控制器内的方法也是有效的。
而使用@controllerAdvice(控制器通知)注解后。这些@ExceptionHandler、@InitBinder、@modelAttribute这些全局注解
更方便的统一管理。
@ExceptionHandler:用户全局处理控制里的异常。
@InitBider:用户设置WebDataBinder,WebDataBinder用来自动绑定前台求求参数到Model中。
@ModelAttribute:@ModelAttribute本来的作用就是绑定键值对到Model里。此处是让全局的@RequestMapping都能获得在此处设置的键值对。
案例:使用@ExceptionHandler处理全局异常,更人性化的将异常输出给用户。
1.@ControllerAdvice声明一个控制器建言,@ControllerAdvice组合了@Component注解,
所以自动注册未spring的Bean。
2.@ExceptionHandler在此处定义全局异常处理,通过@ExceptionHandler的value属性可以过滤拦截的条件,在此处拦截所有Exception。
3.此处使用@ModelAttribute注解将键值对天骄到全局,所有注解的@RequestMapping的方法可都可以获得此键值对。
异常展示
对于注解了@Controller的类,该类下可以使用@ExceptionHandler、@InitBinder、@modelAttribute。@RequestMapping的控制器内的方法也是有效的。
而使用@controllerAdvice(控制器通知)注解后。这些@ExceptionHandler、@InitBinder、@modelAttribute这些全局注解
更方便的统一管理。
@ExceptionHandler:用户全局处理控制里的异常。
@InitBider:用户设置WebDataBinder,WebDataBinder用来自动绑定前台求求参数到Model中。
@ModelAttribute:@ModelAttribute本来的作用就是绑定键值对到Model里。此处是让全局的@RequestMapping都能获得在此处设置的键值对。
案例:使用@ExceptionHandler处理全局异常,更人性化的将异常输出给用户。
package com.boot.springmvc.advice;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice //1
public class ExceptionHandlerAdvice {
@ExceptionHandler(value = Exception.class)//2
public ModelAndView exception(Exception exception, WebRequest request) {
ModelAndView modelAndView = new ModelAndView("error");// error页面
modelAndView.addObject("errorMessage", exception.getMessage());
return modelAndView;
}
@ModelAttribute //3
public void addAttributes(Model model) {
model.addAttribute("msg", "额外信息"); //3
}
@InitBinder //4
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.setDisallowedFields("id"); //5
}
}
一:ExceptionHandlerAdvice类
1.@ControllerAdvice声明一个控制器建言,@ControllerAdvice组合了@Component注解,
所以自动注册未spring的Bean。
2.@ExceptionHandler在此处定义全局异常处理,通过@ExceptionHandler的value属性可以过滤拦截的条件,在此处拦截所有Exception。
3.此处使用@ModelAttribute注解将键值对天骄到全局,所有注解的@RequestMapping的方法可都可以获得此键值对。
4.通过@InitBinder注解定制WebDataBinder。(5.这里表示过滤掉id)
二:抛出异常演示
package com.boot.springmvc.web.ch2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.boot.springmvc.domain.DemoObj;
@Controller
public class AdviceController {
@RequestMapping("/advice")
public String getSomething(@ModelAttribute("msg") String msg,DemoObj obj){//1
System.out.println("//演示@ModelAttribute");
throw new IllegalArgumentException("非常抱歉,参数有误/"+"来自@ModelAttribute:"+ msg);
}
}
三:异常页面展示
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>@ControllerAdvice Demo</title>
</head>
<body>
${errorMessage}
</body>
</html>
结果:
全局的@ModelAttribute,@RequestMapping都可以获取
webDataBinder.setDisallowedFields("id"); //过滤id属性
异常展示