美文网首页
SpringMVC拦截器

SpringMVC拦截器

作者: 夏睡醒了秋 | 来源:发表于2019-06-16 15:40 被阅读0次

1. 创建类实现HandlerInterceptor接口,重写需要的方法。

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

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle start...");
        return true;
    }
}

2. 配置拦截器类(springmvc.xml)

<!--配置拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--拦截-->
            <mvc:mapping path="/*"/>
            <!-- <mvc:exclude-mapping path=""/>  不拦截-->

            <bean class="cn.test.interceptor.MyInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

3. 配置多个拦截器

1. 定义新类
2. 配置新的拦截器

相关文章

网友评论

      本文标题:SpringMVC拦截器

      本文链接:https://www.haomeiwen.com/subject/bpdgfctx.html