SpringMVC的异常处理
1.异常处理思路
- Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServlet找异常处理器进行异常的处理。
2. SpringMVC的异常处理
抛出异常
@RequestMapping("testException")
public String testException()throws SysException{
System.out.println("testException执行了");
try {
//模拟异常
int a=1/0;
} catch (Exception e) {
//打印异常信息
e.printStackTrace();
//抛出自定义异常信息
throw new SysException("查询所有用户出错");
}
return "success";
}
1.自定义异常类
/**
* 自定义异常类
*/
public class SysException extends Exception {
//存储提醒信息
private String messgae;
public SysException(String messgae) {
this.messgae = messgae;
}
public String getMessgae() {
return messgae;
}
public void setMessgae(String messgae) {
this.messgae = messgae;
}
}
- 自定义异常处理器
/**
* 异常处理器
*/
public class SysExceptionResolver implements HandlerExceptionResolver {
/**
* 处理异常的业务逻辑
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param e
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
//获取异常对象
SysException exception = null;
if (e instanceof SysException) {
exception = (SysException) e;
} else {
exception = new SysException("系统正在维护...");
}
//创建ModelAndView
ModelAndView mv = new ModelAndView();
mv.addObject("errorMsg", exception.getMessgae());
mv.setViewName("error");
return mv;
}
}
- 配置异常处理器
<!-- 配置异常处理器 -->
<bean id="sysExceptionResolver" class="com.itcast.exception.SysExceptionResolver"/>
SpringMVC框架中的拦截器
1. 拦截器的概述
- SpringMVC框架中的拦截器用于对处理器进行预处理和后处理的技术。
- 可以定义拦截器链,连接器链就是将拦截器按着一定的顺序结成一条链,在访问被拦截的方法时,拦截器链中的拦截器会按着定义的顺序执行。
- 拦截器和过滤器的功能比较类似,有区别
- 过滤器是Servlet规范的一部分,任何框架都可以使用过滤器技术。
- 拦截器是SpringMVC框架独有的。
- 过滤器配置了/*,可以拦截任何资源。
- 拦截器只会对控制器中的方法进行拦截。
- 拦截器也是AOP思想的一种实现方式
- 想要自定义拦截器,需要实现HandlerInterceptor接口。
2. 自定义拦截器步骤
- 创建类,实现HandlerInterceptor接口,重写需要的方法
/**
* 预处理 controller方法执行前 执行
* return true放行 执行下一个拦截器 没有执行controller
* return false 不放行 跳到另外页面
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor1执行............");
return true;
}
- 在springmvc.xml中配置拦截器类
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor> <!-- 哪些方法进行拦截 -->
<mvc:mapping path="/user/*"/>
<!-- 哪些方法不进行拦截 <mvc:exclude-mapping path=""/> -->
<!-- 注册拦截器对象 -->
<bean class="com.itcast.interceptor.MyInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
3. HandlerInterceptor接口中的方法
- preHandle方法是controller方法执行前拦截的方法
- 可以使用request或者response跳转到指定的页面
- return true放行,执行下一个拦截器,如果没有拦截器,执行controller中的方法。
- return false不放行,不会执行controller中的方法。
- postHandle是controller方法执行后执行的方法,在JSP视图执行前。
- 可以使用request或者response跳转到指定的页面
- 如果指定了跳转的页面,那么controller方法跳转的页面将不会显示。
- postHandle方法是在JSP执行后执行
- request或者response不能再跳转页面了
配置多个拦截器
再编写一个拦截器的类
public class MyInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor2执行preHandle............");
return true;
}
/**
* controller 执行后 success执行前
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor2执行postHandle............后");
}
}
- 配置2个拦截器
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor> <!-- 哪些方法进行拦截 -->
<mvc:mapping path="/user/*"/>
<!-- 哪些方法不进行拦截 <mvc:exclude-mapping path=""/> -->
<!-- 注册拦截器对象 -->
<bean class="com.itcast.interceptor.MyInterceptor1"/>
</mvc:interceptor>
<mvc:interceptor> <!-- 哪些方法进行拦截 -->
<mvc:mapping path="/user/*"/>
<!-- 哪些方法不进行拦截 <mvc:exclude-mapping path=""/> -->
<!-- 注册拦截器对象 -->
<bean class="com.itcast.interceptor.MyInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>