1. 自定义拦截器

  实现HandlerInterceptor接口

拦截器一:

package cn.rodge.ssm.interceptor;

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

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * 自定义拦截器1
 * @author Rodge
 *
 */
public class Interceptor_First implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //判断用户是否登录,即session中是否有值
        //如果用户登录,放行return true
        //如果没登录,页面转发,拦截
        System.out.println("Interceptor_first preHandler");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("Interceptor_first postHandler");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("Interceptor_first afterCompletion");

    }

}

拦截器二:

public class Interceptor_Second implements HandlerInterceptor {......}

拦截器三

public class Interceptor_Third implements HandlerInterceptor {......}

2. 拦截器的配置

  2.1 针对某种mapping进行拦截

<bean

class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">

    <property name="interceptors">

       <list>

           <ref bean="handlerInterceptor1"/>

           <ref bean="handlerInterceptor2"/>

       </list>

    </property>

</bean>

    <bean id="handlerInterceptor1" class="springmvc.intercapter.HandlerInterceptor1"/>

    <bean id="handlerInterceptor2" class="springmvc.intercapter.HandlerInterceptor2"/>

  2.2   全局拦截器

<!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.rodge.ssm.interceptor.Interceptor_First"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.rodge.ssm.interceptor.Interceptor_Second"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.rodge.ssm.interceptor.Interceptor_Third"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

web.xml中的配置

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

<!--  需要在前端控制器中添加过滤路径  -->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

3. 拦截器的执行流程

preHandle按拦截器定义顺序调用

postHandler按拦截器定义逆序调用

afterCompletion按拦截器定义逆序调用

 

postHandler在拦截器链内所有拦截器返成功调用

afterCompletion只有preHandle返回true才调用

 

4. 拦截器应用实例

  用户身份认证

  4.1 拦截器的编写

package cn.rodge.ssm.interceptor;

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

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * 自定义拦截器1
 * @author Rodge
 *
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //如果是登录界面,放行
        String requestURI = request.getRequestURI();
        System.out.println("请求uri路径" + requestURI);
        if (requestURI.contains("login")) {
            return true;
        }
        //判断用户是否登录,即session中是否有值
        HttpSession session = request.getSession();
        Object object = session.getAttribute("username");
        if (object != null) {
            //如果用户登录,放行return true
            return true;
        } else {
            //如果没登录,页面转发,拦截
            System.out.println("工程名:   " + request.getContextPath());
            request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("Interceptor_Third postHandler");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("Interceptor_Third afterCompletion");

    }

}
  4.2 拦截器的配置

在springmvc.xml中的配置

 <!-- 配置静态资源访问:解决springmvc拦截js的问题 -->
    <mvc:resources location="/js/" mapping="/js/**"/>
   

   <!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.rodge.ssm.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

web.xml中的配置

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

 

posted on 2017-03-13 23:32  rodge  阅读(348)  评论(0编辑  收藏  举报