27.自定义拦截器
Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口1.自定义实现类实现 HandlerInterceptor接口
public class MyInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
request.setAttribute("test", "after");
System.out.println("[first]afterConpletion执行了");
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
//request.setAttribute("test", "post");
System.out.println("[first]postHandle执行了");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("test", "pre");
System.out.println("[first]preHandle执行了");
return true;
}
}
<mvc:interceptors>
<bean class="com.neuedu.interceptor.MyInterceptor"></bean>
</mvc:interceptors>
– preHandle():该方法在目标方法之前被调用,
若返回值为true,则继续调用后续的拦截器和目标方法
若返回值为false,则不会再调用后续的拦截器和目标方法
可以考虑做权限,日志或者事务等!
– postHandle():调用目标方法之后,但渲染视图之前被调用
可以对请求域中的属性或者视图做出修改!
– afterCompletion():渲染视图之后被调用,
可以在该方法中进行一些资源清理的操作。
preHandle() - > controller - > postHandle - > 视图(页面)- > afterCompletion
关于拦截器的配置:
<mvc:interceptors>
<!--配置自定义拦截器 ,所有的路径都拦截 -->
<bean class="com.neuedu.springmvc.filter.MyFilter"></bean>
<!-- 关于拦截器的配置问题 ,可以通过<mvc:exclude-mapping path=""/>来配置使用哪个拦截器来【不】拦截的路径-->
<mvc:interceptor>
<!-- 用<mvc:mapping>标签指定要拦截的路径 -->
<mvc:mapping path="/employ"/>
<!-- 指定使用哪个拦截器进行拦截 -->
<bean class="com.neuedu.springmvc.filter.SecondFilter"></bean>
</mvc:interceptor>
</mvc:interceptors>
关于内外拦截器的顺序:
在上边配置的拦截器中,内部的拦截器函数在控制台输出[second]前缀,外部的myInterceptor输出[first]前缀
26.SpringMVC的异常处理:
1.在springmvc配置文件中加上<mvc:annotation-driven>标签:
2.在当前Handler中定义由@ExceptionHandler注解修饰的方法,用于处理异常信息!
注意:
1.@ExceptionHandler方法修饰的入参中可以加入Exception类型的参数,该参数即对应发生的异常信息
2.@ExceptionHandler方法的入参中不能传入Map.若希望把异常信息传到页面上,需要使用ModelAndView作为方法的返回值。
3.@ExceptionHandler 注解定义的方法优先级问题:
例如发 生的是NullPointerException,但是声明的异常有 RuntimeException 和 Exception,此候会根据异常的最近 继承关系找到继承深度最浅的那个 @ExceptionHandler 注解方法,即标记了 RuntimeException 的方法
@ExceptionHandler(value= {Exception.class})
public ModelAndView getEx(Exception exception) {
String viewName="error";
ModelAndView mv = new ModelAndView(viewName);
mv.addObject("error", exception);
return mv;
}
4.ExceptionHandlerMethodResolver 内部若找不 到@ExceptionHandler 注解的话,会找@ControllerAdvice 中的@ExceptionHandler 注解方法
@ControllerAdvice
public class MyException {
@ExceptionHandler(value= {Exception.class})
public ModelAndView testEx(Exception exception) {
String viewName="error";
ModelAndView mv = new ModelAndView(viewName);
mv.addObject("error", exception);
return mv;
}
}
27.基于配置的异常处理:
如果希望对所有异常进行统一处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为
视图名,即发生异常时使用对应的视图报告异常
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 指定在request域中获取异常信息所需要的key:即ex,若不指定,默认为exception -->
<property name="exceptionAttribute" value="ex"></property>
<!-- 置顶异常映射 -->
<property name="exceptionMappings">
<props>
<!-- 由prop标签的key属性指定发生异常的全类名,由值指定出现异常去哪个页面 -->
<prop key="java.lang.ArithmeticException">error</prop>
</props>
</property>
</bean>
28.SpringMVC的执行顺序