springmvc拦截器和异常处理器

1 拦截器

拦截器类似于servlet中的过滤器,用于进行预处理和后处理。将拦截器按一定的顺序连成一条链,组成拦截器链。在访问被拦截的资源时,拦截器链中的拦截器会按之前定义的顺序被调用。拦截器的底层原理也是动态代理。

自定义拦截器

实现HandlerInterceptor的接口方法:

public class InterceptorTest implements HandlerInterceptor {
    //请求处理之前被调用,可以校验登录信息或者一些特定信息
    //返回为false时,请求结束,后面的Interceptor和Controller不会再执行;
    //返回值true时,继续调用下一个Interceptor的preHandle 方法,如果没有下一个Interceptor,则执行Controller中的方法。
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("前置处理");
        String user = request.getParameter("user");
        if("user".equals(user)){
            return true;
        }
        return false;
    }

    //请求处理之后被调用,视图渲染之前(doDispatch方法中)
    //可以对Controller处理之后的ModelAndView对象进行一些操作
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("后置处理");
    }

    //整个请求结束之后被调用,视图渲染之后
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("处理完了");
    }
}

spring-mvc.xml文件中添加拦截器配置:

 <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.test.interceptor.InterceptorTest2"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.test.interceptor.InterceptorTest"/>
        </mvc:interceptor>
    </mvc:interceptors>

配置的先后决定了拦截器链上拦截器的顺序,即先InterceptorTest2,后InterceptorTest。

2 异常处理器

springmvc的异常处理器主要用来处理后台抛出异常情况下,前端页面能够友好显示。

2.1 内置异常处理器

SimpleMappingExceptionResolver简单映射异常处理器:
spring-mvc.xml文件中添加配置:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
     <!--出现除exceptionMappings配置的异常外,显示error.jsp页面-->
     <property name="defaultErrorView" value="error.jsp"/>
     <property name="exceptionMappings">
         <map>
             <!--出现ClassNotFoundException异常时,显示error.jsp页面-->
             <entry key="java.lang.ClassNotFoundException" value="error.jsp"/>
         </map>
     </property>
 </bean>

注意:如果有配置过视图解析器,如下述配置,则value的值,不需要加.jsp

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>

2.2 自定义异常处理器

public class ExceptionTest implements HandlerExceptionResolver {

    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();
        //不同的异常类型,展示不同的界面
        if(ex instanceof ClassNotFoundException){
            modelAndView.setViewName("error.jsp");
        }else{
            modelAndView.setViewName("other.jsp");
        }
        return modelAndView;
    }
}

spring-mvc.xml中添加配置:

<bean id="exceptionResolver" class="com.test.ExceptionTest"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值