SpringMVC拦截器和异常处理机制

SpringMVC拦截器和异常处理机制

概念

SpringMVC拦截器类似于过滤器,用于进行预处理和后处理

将拦截器按照一定顺序连接成一条链,就是拦截器链

拦截器和过滤器区别

拦截器的快速入门

  1. 创建拦截器类实现HandlerInterceptor接口

  2. 配置拦截器

    <!--    配置拦截器-->
        <mvc:interceptors>
            <mvc:interceptor>
    <!--            对那些资源执行拦截操作,这是对所有资源执行-->
                <mvc:mapping path="/**/"/>
                <bean class="com.myspring.interceptor.MyHandlerInterceptor"></bean>
            </mvc:interceptor>
        </mvc:interceptors>
    
  3. 测试拦截器的拦截效果

    package com.myspring.interceptor;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class MyHandlerInterceptor implements HandlerInterceptor {
    //    覆盖三个方法
    //在目标方法(控制类中对应方法)执行前执行
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            String param = request.getParameter("param");
            if("yes".equals(param)){
                //        返回值为false不放行,true放行
                return true;
            }
            else {
                request.getRequestDispatcher("/error.js").forward(request,response);
                return  false;
            }
        }
    //在目标方法执行之后,视图返回之前执行
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            modelAndView.addObject("name","xiaoming");
        }
    //在整个流程都执行完毕后执行
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            
        }
    }
    

三个方法的执行:在配置文件中依次配置两个拦截器分别为1和2,则执行顺序是:1的 preHandle >2的preHandle>2的postHandle>1的postHandle>2的afterCompletion>1的afterCompletion

SpringMVC异常处理机制

如图:

异常处理的两种方式:

  • 使用springmvc通过的简单异常处理器SimpleMappingExceptionResolver
  • 使用Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理器

SimpleMappingExceptionResolver

主要需要进行相应异常与视图的映射配置

在springmvc的配置文件中:

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--        默认错误页面-->
        <property name="defaultErrorView" value="error"/>
        <property name="exceptionMappings">
            <map>
                <entry key="java.lang.ClassCastException" value="error1"/>
                <entry key="java.lang.ClassNotFoundException" value="error2"/>
            </map>
        </property>
    </bean>

HandlerExceptionResolver自定义异常处理器

  1. 创建异常处理器类实现HandlerExceptionResolver

    public class MyExceptionResolver implements HandlerExceptionResolver {
        @Override
        //参数Exception是异常对象,返回值是你要跳转的错误页面
        public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
            ModelAndView modelAndView = new ModelAndView();
            if(e instanceof  Exception){
    
            }
            modelAndView.setViewName("error.jsp");
            return modelAndView;
        }
    }
    
  2. 配置异常处理器

    <bean class="com.myspring.domain.MyExceptionResolver"/>
    
  3. 编写异常页面

热门相关:洪荒二郎传   金粉   金粉   锦庭娇   神算大小姐