전처리 후처리를 할 수 있는 인터셉터를 만들어줘보자.
스프링은 전처리 후처리를 담당 할 수 있는 인터셉터란 녀석이있다.
먼저 WEB-INF의 servlet-context를변경해주자
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <mvc:annotation-driven /> <context:component-scan base-package="com.pratice.project" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.pratice.project.interceptor.CommonInterceptor" /> </mvc:interceptor> </mvc:interceptors> <!-- 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.pratice.project"></context:component-scan> --> </beans> |
보면 알다시피 /**란 뜻은 prefix인 project를 포함한 모든 url매핑의 전처리를 뜻한다 이부분을 유저가 정의해서 따로 인터셉터를 만들수도있다. 다음은 인터셉터 클래스인 CommonInterceptor를 만들어주자.물론이전에 interceptor란 패키지를 만들어주자
package com.pratice.project.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class CommonInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object Obj) throws Exception { // TODO Auto-generated method stub System.out.println("prehandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object Obj, ModelAndView mv) throws Exception { // TODO Auto-generated method stub System.out.println("posthandle");
} @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object Obj, Exception ex) throws Exception { // TODO Auto-generated method stub
}
} |
prehandle은 전처리를 의미한다. posthandle은 후처리를 의미하고 마지막 aftercomplete은 로직이 종료됬을때를의미한다.
그리고 파라미터를 보면 request와 response가 있는것을 확인 할 수 있다. 전처리 후처리에 출력문을 적어두었다.
여기서 주의할점은 전처리에 true를 리턴해야만 정상적으로 전처리가 실행된다.
정상적이라면 아래와같은 구조를 띄게된다.
그다음 할일은 서버를 돌려서 아무 API나 호출해보자
그다음 출력창을 확인해보자.
정상적으로 전처리 후처리가 되는것을 확인할 수있다.
'DEV > SPRING' 카테고리의 다른 글
| Rest Service 구현[10] (0) | 2018.05.11 |
|---|---|
| Rest Service 구현[8] (0) | 2018.05.11 |
| Rest Service 구현[7] (0) | 2018.05.11 |
| Rest Service 구현[6] (0) | 2018.05.11 |
| Rest Service 구현[5] (0) | 2018.05.11 |