ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring - Interceptor 적용
    기초/SPRING 2020. 8. 28. 14:21

    action-servlet.xml 수정

    맨위 beans url 추가

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    beans에 인터셉터태그 추가

    	<!-- Interceptors -->
    	<mvc:interceptors>
    		<mvc:interceptor>
    			<mvc:mapping path="/**" /> 
    			<beans:bean class="com.donggyu.project.comm.logger.CustomInterceptor" />
    		</mvc:interceptor>
    	</mvc:interceptors>

    <적용된 화면>

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"
    	xmlns:mvc="http://www.springframework.org/schema/mvc">
    	
        <!-- 나머지 생략 -->
    
    	<!-- Interceptors -->
    	<mvc:interceptors>
    		<mvc:interceptor>
    			<mvc:mapping path="/**" /> 
    			<beans:bean class="com.donggyu.project.comm.logger.CustomInterceptor" />
    		</mvc:interceptor>
    	</mvc:interceptors>
    	
    </beans:beans>

     

    CustomInterceptor.java 생성

    comm > logger > CustomInterceptor.java

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    public class CustomInterceptor extends HandlerInterceptorAdapter {
    	protected Log log = LogFactory.getLog(CustomInterceptor.class);
    
    	@Override
    	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    			throws Exception {
    		log.info("====================================== START ======================================");
    		log.info(" Request URI \t: " + request.getRequestURI());
    
    		return super.preHandle(request, response, handler);
    	}
    
    	@Override
    	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
    			ModelAndView modelAndView) throws Exception {
    		log.info("====================================== END ======================================\n");
    		super.postHandle(request, response, handler, modelAndView);
    	}
    
    	@Override
    	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
    			throws Exception {
    		log.info("====================================== afterCompletion ======================================\n");
    		super.afterCompletion(request, response, handler, ex);
    	}
    
    }
    

     

    실행결과 확인

    INFO : com.donggyu.project.comm.logger.CustomInterceptor - ====================================== START ======================================
    INFO : com.donggyu.project.comm.logger.CustomInterceptor -  Request URI 	: /index.dg
    INFO : com.donggyu.project.comm.HomeController - Welcome home! The client locale is ko_KR.
    INFO : com.donggyu.project.comm.logger.CustomInterceptor - ====================================== END ======================================
    
    INFO : com.donggyu.project.comm.logger.CustomInterceptor - ====================================== afterCompletion ======================================

     

    '기초 > SPRING' 카테고리의 다른 글

    [Spring] Mysql 계층적 쿼리구조  (0) 2020.09.23
    [Spring] Maria DB 연결+SQL LOG  (0) 2020.09.09
    Spring - Servelt 설정변경  (0) 2020.08.28
    부트스트랩 적용 및 resources 지정  (0) 2020.08.28
    타일즈 적용  (0) 2020.08.26

    댓글

Designed by Tistory.