Rhythmk 一步一步学 JAVA(4):Spring MVC -之拦截器

1、实现拦截器类(myInterceptor):

package com.rhythmk.Interceptor;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.rhythmk.myspringmvc.HomeController;

/*
 *   过滤器
 */

public class myInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(myInterceptor.class);
    
    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
        System.out.println("print preHandle"); 
        
        logger.info("preHandle");
        return true;  
    }  
    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {  
       
        logger.info("postHandle");
        System.out.println("print postHandle");  
    }  
    @Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {  
        logger.info("afterCompletion");
        System.out.println("print afterCompletion");  
    }  
    
    
    
}

 

2、添加拦截器配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">



    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->

    <mvc:annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <!--<resources mapping="/resources/**" location="/resources/" /> -->

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->

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


    <context:component-scan base-package="com.rhythmk.myspringmvc" />

    <!-- 拦截器 -->

    <mvc:interceptors>

        <mvc:interceptor>

            <mvc:mapping path="/*" />

            <bean class="com.rhythmk.Interceptor.myInterceptor" />

        </mvc:interceptor>

    </mvc:interceptors>


</beans>

 

输出:

===========print preHandle

===========print postHandle
===========print afterCompletion

posted @ 2014-03-16 23:10  Rhythmk  阅读(348)  评论(0编辑  收藏  举报
Rhythmk 个人笔记