参考资料
@ControllerAdvice
,是Spring3.2提供的新注解,它是一个Controller增强器,可对controller进行增强处理。
@ExceptionHandler
注解,进行全局异常处理。@InitBinder
注解,用来设置WebDataBinder
,用于自动绑定前台请求参数到Model中,全局数据预处理,多用于表单提交数据或者url传参。@ModelAttribute
注解,让Controller类中所有的方法都可以获取到通过@ModelAttribute
注解设置的值,进行全局数据绑定。❗❗❗注意:@ControllerAdvice
注解将作用在所有Controller层的方法上。
⏹作用:增强指定.class
的类,非指定的Controller类不会被增强
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// 指定增强S001LoginController和S002LogoutController
@ControllerAdvice(assignableTypes = {S001LoginController.class, S002LogoutController.class})
public class S_Controller {
@Autowired
private HttpServletRequest request;
// 方式1: @ModelAttribute注解的使用
@ModelAttribute
public void initData1(Model model) {
request.setAttribute("num1", 66);
model.addAttribute("userMail", List.of("[email protected]", "[email protected]"));
}
// 方式2: @ModelAttribute注解的使用
@ModelAttribute(name = "userInfo")
public Map<String, String> initData2() {
request.setAttribute("num2", 99);
return new HashMap<>(){
{
put("name", "贾飞天");
put("age", "18");
}
};
}
// 捕获S001LoginController或S002LogoutController类中的Exception异常
@ExceptionHandler(Exception.class)
public void exceptionHandle(Exception ex) {
System.out.println(ex.getMessage());
}
}
⏹目录结构如下
⏹说明
@ControllerAdvice增强类
中,指定对S001LoginController.java和S002LogoutController.java进行了增强,因此这两个类中可以获取到增强类中放入的数据,并且这两个类中抛出的Exception异常
也会被增强类所捕获。⭕S001LoginController.java
@ControllerAdvice
增强类中放入Model中的数据
model.asMap()
@ModelAttribute("key名称") 类型 变量名
request.getAttribute("key名称")
来获取放入request
的attribute
中的数据。import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/ss001")
public class S001LoginController {
@Autowired
private HttpServletRequest request;
@GetMapping("/init")
public ModelAndView init(Model model) {
// ⭕方式1: 获取出@ControllerAdvice增强类中提前放入Model中的数据
Map<String, Object> mapInfo = model.asMap();
Object userMailObj = mapInfo.get("userMail");
List<String> userMailList = (List<String>)userMailObj;
System.out.println(userMailList); // [[email protected], [email protected]]
Map<String, String> userInfoMap = (Map<String, String>) mapInfo.get("userInfo");
System.out.println(userInfoMap); // {name=贾飞天, age=18}
// ⭕获取出@ControllerAdvice增强类中提前放入request的attribute中的数据
Integer num1 = (Integer) request.getAttribute("num1");
System.out.println(num1); // 66
Integer num2 = (Integer) request.getAttribute("num2");
System.out.println(num2); // 99
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("s001");
return modelAndView;
}
@GetMapping("/showInfo")
@ResponseBody
public void showInfo(
// ⭕方式2: 获取出@ControllerAdvice增强类中提前放入Model中的数据
@ModelAttribute("userMail") List<String> userMail
, @ModelAttribute("userInfo") Map<String, String> userInfo
) {
System.out.println(userMail); // [[email protected], [email protected]]
System.out.println(userInfo); // {name=贾飞天, age=18}
}
}
⭕S002LogoutController.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
@Controller
@RequestMapping("/ss002")
public class S002LogoutController {
@GetMapping("/init")
public ModelAndView init(Model model) {
// 获取出@ControllerAdvice增强类中提前放入Model中的数据
Map<String, Object> mapInfo = model.asMap();
System.out.println(mapInfo);
// 代码出现异常
int num = 1 / 0;
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("s002");
return modelAndView;
}
}
⭕S003Controller.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/ss003")
public class S003Controller {
@GetMapping("/init")
public ModelAndView init() {
int num = 1 / 0;
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("s003");
return modelAndView;
}
}
⏹获取数据
⏹异常
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice(basePackages = {"com.example.jmw.t_controller"})
public class T_Controller {
@ExceptionHandler(Exception.class)
public void exceptionHandle(Exception ex) {
System.out.println(ex.getMessage());
}
}
指定注解
的所有的Controllerimport java.lang.annotation.*;
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ControllerMarkAnnotation {
}
mport org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice(annotations = ControllerMarkAnnotation.class)
public class X_Controller {
@ExceptionHandler(Exception.class)
public void exceptionHandle(Exception ex) {
System.out.println(ex.getMessage());
}
}