1.构建异常类
public class CustomeException extends RuntimeException{
public CustomeException() {
super();
// TODO Auto-generated constructor stub
}
public CustomeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public CustomeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CustomeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CustomeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
2.实现HandlerExceptionResolver接口
public class GlobalExceptionResolver implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse rep, Object obj, Exception exc) {
String msg="";
if(exc instanceof CustomeException){
msg="客户异常";
} else {
msg="管理员";
}
ModelAndView mav=new ModelAndView();
mav.addObject("msg",msg);
mav.setViewName("error");
return mav;
}
}
3.配置springMvc.xml文件
<bean class="HandlerExceptionResolver实现类路径">
网友评论