程序员社区

SpringMvc中拦截器

文章目录

    • 1.拦截器概述
    • 2.拦截器类中的方法
      • 1.先写一个前端页面
      • 2.写后台代码
      • 3.编写success.jsp页面
      • 4.编写拦截器类,实现HandlerInterceptor接口
      • 5.编写error.jsp页面
      • 6.配置拦截器类
    • 3.配置多个拦截器
      • 3.1再写一个拦截器类
      • 3.2 配置拦截器类

1.拦截器概述

  1. SpringMVC框架中的拦截器用于对处理器进行预处理和后处理的技术。
  2. 可以定义拦截器链,连接器链就是将拦截器按着一定的顺序结成一条链,在访问被拦截的方法时,拦截器链
    中的拦截器会按着定义的顺序执行。
  3. 拦截器和过滤器的功能比较类似,有区别
     过滤器是Servlet规范的一部分,任何框架都可以使用过滤器技术。
     拦截器是SpringMVC框架独有的。
     过滤器配置了/*,可以拦截任何资源。
     拦截器只会对控制器中的方法进行拦截。
  4. 拦截器也是AOP思想的一种实现方式
  5. 想要自定义拦截器,需要实现HandlerInterceptor接口。

2.拦截器类中的方法

  1. preHandle方法是controller方法执行前拦截的方法
     可以使用request或者response跳转到指定的页面
     return true放行,执行下一个拦截器,如果没有拦截器,执行controller中的方法。
     return false不放行,不会执行controller中的方法。
  2. postHandle是controller方法执行后执行的方法,在JSP视图执行前。
     可以使用request或者response跳转到指定的页面
     如果指定了跳转的页面,那么controller方法跳转的页面将不会显示。
  3. postHandle方法是在JSP执行后执行
      request或者response不能再跳转页面了

1.先写一个前端页面

<a href="/user/testInterceptor">拦截器</a>

2.写后台代码

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/testInterceptor")
    public String testInterceptor(){
        System.out.println("controller...");
        return "success";
    }
}

3.编写success.jsp页面

<h3>执行成功</h3>

4.编写拦截器类,实现HandlerInterceptor接口

public class MyInterceptor1 implements HandlerInterceptor {
    /**
     * 预处理
     * return true放行,执行下一个拦截器,如果没有,执行controller中的方法
     * return false不放行,不会执行controller方法
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor执行了....前111");
        //request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request,response);
        return true;
    }

    /**
     *后处理方法,controller方法执行之后,success.jsp执行之前执行
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor执行了....后111");
        request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request,response);
    }

    /**
     * success.jsp跳转页面执行后,执行的方法
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor执行了....最后111");
    }
}

5.编写error.jsp页面

<h3>错误页面</h3>

6.配置拦截器类

<!--配置拦截器111-->
<mvc:interceptors>
    <mvc:interceptor>
        <!--要拦截的方法-->
        <mvc:mapping path="/user/*"/>
        <!--不要拦截的方法-->
        <!--<mvc:exclude-mapping path=""/>-->
        <!-- 配置拦截对象-->
        <bean class="com.hh.interceptor.MyInterceptor1"/>
    </mvc:interceptor>
</mvc:interceptors>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.配置多个拦截器

3.1再写一个拦截器类

public class MyInterceptor2 implements HandlerInterceptor {
    /**
     * 预处理
     * return true放行,执行下一个拦截器,如果没有,执行controller中的方法
     * return false不放行,不会执行controller方法
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor执行了....前222");
        //request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request,response);
        return true;
    }

    /**
     *后处理方法,controller方法执行之后,success.jsp执行之前执行
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor执行了....后222");
        request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request,response);
    }

    /**
     * success.jsp跳转页面执行后,执行的方法
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor执行了....最后222");
    }
}

3.2 配置拦截器类

    <!--配置拦截器222-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--要拦截的方法-->
            <mvc:mapping path="/**"/>
            <!-- 配置拦截对象-->
            <bean class="com.hh.interceptor.MyInterceptor2"/>
        </mvc:interceptor>
    </mvc:interceptors>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

赞(0) 打赏
未经允许不得转载:IDEA激活码 » SpringMvc中拦截器

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区