728x90
반응형
필터와 비슷한 역할을 하는 객체
요청을 처리하기 전, 요청을 가로채 먼저 확인한후 처리한다
ex) 로그인이 되어 있어야 메인 페이지를 보여주도록 처리할 때 사용
▶ 사용 방법
1) 요청을 가로채 동작을 수행할 인터셉터 클래스 작성하기
- 인터셉터 클래스는 HandlerInterceptorAdapter를 상속 받아서 구현
2) 스프링 bean 생성하기
<bean id = "transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
3) transaction을 Annotation(@Transactional)을 이용해서 적용할 것임을 선언하기
<tx:annotation-driven transaction-manager="transactionManager"/>
4)Transaction을 적용할 메서드 또는 클래스위에
@Transactional 을 선언하기
5) 인터셉터가 가로챌 요청 명시하기
: 현재 코드는 인터셉터를 이용한 로그인 처리하는 예제로
- 로그인이 되어있지 않으면, 특정요청을 실행할 수 없도록 한다
1. LoginCheckInterceptor 를 생성하여
- 로그인이 되어있지 않으면 지정한 요청을 처리하지 않고, 로그인 화면으로 돌려보내도록 작성하였다
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/member/main"/>
<mvc:mapping path="/member/memberList"/>
<mvc:mapping path="/member/modify"/>
<mvc:mapping path="/member/logout"/>
<ref bean="loginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
반응형
댓글