方式一
步骤一:创建异常类
public class GyException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
private String exception;
public GyException() {}
public GyException(String exception) {
super(exception);
this.exception =exception;
}
public String getException() {
return exception;
}
public void setException(String exception) {
this.exception = exception;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "GyException [exception=" + exception+" ]";
}
}
步骤二: 创建顶层异常处理类
public class BaseExceptionController {
@ExceptionHandler
public ModelAndView resolveException(Exception ex) {
ModelAndView mv = new ModelAndView();
System.out.println("i am com in 1");
mv.setViewName("custom_error");
if (ex instanceof GyException) {
ex = (GyException)ex;
mv.addObject("message", ex.getMessage());
System.out.println("i am com in 2");
mv.setViewName("custom_error");
} else if (ex instanceof RuntimeException) {
System.out.println("i am com in 3");
mv.setViewName("custom_error");
}
return mv;
}
}
步骤三:需要处理异常的controller类,集成基类异常处理类
public class UserController extends BaseExceptionController {
@RequestMapping(value="login")
public String login(HttpServletRequest request,HttpServletResponse response) {
return "register";
}
@RequestMapping(value="add")
public void add(HttpServletRequest request,HttpServletResponse response,User user) {
AddUser adduser =new AddUser();
adduser.addUser(user);
}
@RequestMapping(value="exception")
public String ecxeption(HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse ,User user) throws Exception {
if(user.getUserName()==null) {
System.out.println("用户名不能为空");
throw new GyException("用户名不能为空");
}
return "custom_error";
}
}
@Exception中可以指定该方法处理哪一类异常及其子类。例如:@Exception(value=“value=GyException.class”)value的值值及时处理
该方法处理的具体异常类型。也可以指定多个方法并指定处理指定的特定异常。
如果单独使用@ExceptionHandle,只能在当前controller中处理异常
方式二:@Controller+@ExceptionHandler配合使用完成全局异常捕获
注意:@controller注释的类必须被Spring扫描到注册到spring容器中才能生效。
@ControllerAdvice
是一个@Component
,用于定义@ExceptionHandler
,@InitBinder
和@ModelAttribute
方法,适用于所有使用@RequestMapping
方法
第一步:检查@ControllerAdvice注解的类是否被扫描到
<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.test.validator" />
第二步写异常处理类
@ControllerAdvice
public class BaseExceptionController {
@ExceptionHandler(value=Exception.class)
public ModelAndView resolveException(Exception ex) {
ModelAndView mv = new ModelAndView();
System.out.println("diao yong de shi me ");
mv.setViewName("custom_error");
if (ex instanceof GyException) {
ex = (GyException)ex;
mv.addObject("message", ex.getMessage());
System.out.println("i am com in 2");
mv.setViewName("custom_error");
} else if (ex instanceof RuntimeException) {
System.out.println("i am com in 3");
mv.setViewName("custom_error");
}
return mv;
}
//这里的GyExceptionOne和GyException是 不同类型的自定义异常
@ExceptionHandler(value=GyExceptionOne.class)
public ModelAndView resolveException1(Exception ex) { ModelAndView mv = new ModelAndView(); System.out.println("diao yong de shi wo ao ao"); mv.setViewName("custom_error"); if (ex instanceof GyExceptionOne) { ex = (GyExceptionOne)ex; mv.addObject("message", ex.getMessage()); System.out.println("i am com in 2"); mv.setViewName("custom_error"); } else if (ex instanceof RuntimeException) { System.out.println("i am com in 3"); mv.setViewName("custom_error"); } return mv; }}
第三步:产生异常
@RequestMapping(value="exception")
public String ecxeption(HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse ,User user) throws Exception {
if(user.getUserName()==null) {
System.out.println("用户名不能为空");
throw new GyException("用户名不能为空");
}
return "custom_error";
}
第四步:在JSP页面对异常进行展示