1.目录结构
2.说明
@ControllerAdvice:控制器建言,对控制器全局配置,注解了@Controller的类的方法可使用@ExceptionHandler、@ModelAttribute、@InitBinder注解到方法上,对所有注解了@RequestMapping的控制器内的方法都有效
@ExceptionHandler:用于全局处理控制器的异常
@InitBinder :用来设置WebDataBinder,WebDataBinder用来自动绑定前台请求参数到Model中
@ModelAttribute: 绑定键值对到Model
3.编写
1.实体类
package com.wen.springmvc4.domain;
public class DemoObj {
private Long id;
private String name;
public DemoObj() {
super();
}
public DemoObj(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.定制ControllerAdvice
package com.wen.springmvc4.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//声明一个控制器建言,@ControllerAdvice组合了@Component注解,自动注册为Spring的Bean
public class ExceptionHandlerAdvice {
//@ExceptionHandler在此定义全局处理,通过@ExceptionHandler的value属性可获得可过滤拦截的条件,此处拦截所有Exception
@ExceptionHandler(value=Exception.class)
public ModelAndView exception(Exception exception,WebRequest request){
System.out.println("ExceptionHandlerAdvice-@ExceptionHandler");
ModelAndView modelAndView=new ModelAndView("error");//error页面
modelAndView.addObject("errorMessage",exception.getMessage());
return modelAndView;
}
@ModelAttribute//将键值对添加到全局,所有注解的@RequestMapping的方法可获得此键值对
public void addAttributrs(Model model){
System.out.println("ExceptionHandlerAdvice-@ModelAttribute");
model.addAttribute("msg", "额外消息");
}
@InitBinder//定制webDataBinder
public void initBinder(WebDataBinder webDataBinder){
System.out.println("ExceptionHandlerAdvice-@InitBinder");
//此处忽略request参数参数的id
webDataBinder.setDisallowedFields("id");
}
}
3.控制器
package com.wen.springmvc4.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wen.springmvc4.domain.DemoObj;
@Controller
public class AdviceController {
@RequestMapping("/advice")
public String getSomething(@ModelAttribute("msg") String msg,DemoObj obj){
System.out.println("AdviceController");
throw new IllegalArgumentException("非常抱歉,参数有误/"+"来自@ModelAttribute:"+msg);
}
}
4.异常展示页面
在src/main/resource/views先新建error.jsp
<%@ 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=ISO-8859-1">
<title>@ControllerAdvice Demo</title>
</head>
<body>
${errorMessage}
</body>
</html>
4.运行
将程序部署到Tomcat,运行,访问http://localhost:8080/SpringMVC/advice?id=1&name=2
控制台打印消息 :
可以看出先执行注解了@ModelAttribute、@InitBinder的方法,然后注解了@Controller类的@RequestMapping的方法,最后@ExceptionHandler