블로그 이미지
헤이장

Notice

Recent Post

Recent Comment

Recent Trackback

calendar

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
  • total
  • today
  • yesterday
2009. 7. 27. 20:19 Web/Framework

액션클래스가 실행되기전에 가로채서 어떤 일을 수행하는 인터셉터

우선 보면


<action name="chkLogin" class="action.CheckLoginAction">

            <interceptor-ref name="servletConfig"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="params"/>
            <result>main.jsp</result>
</action>


public class CheckLoginAction implements Preparable, ModelDriven<Member_Hanbit>, SessionAware {
...  생략 ...
}


모든 인터셉터는 정상적으로 작동한다.
 

그렇다면 이렇게 하면?


<action name="chkLogin" class="action.CheckLoginAction">
            <result>main.jsp</result>
</action>

public class CheckLoginAction implements Preparable, ModelDriven<Member_Hanbit>, SessionAware {
             ...  생략 ...
}

이건 또 뭔가...?
액션 클래스에 구현된 모든 인터셉터가 똑같이 정상 작동한다. 세션 조차도...
params 를 안 써도 파라메터도 받아진다


그럼 이건???


<action name="chkLogin" class="action.CheckLoginAction"> 
            <interceptor-ref name="params"/>
            <result>main.jsp</result>
</action>

public class CheckLoginAction implements Preparable, ModelDriven<Member_Hanbit>, SessionAware {
                ...  생략 ...
}

다른 인터셉터는 작동하지 않고 파라메터 인자만 받아진다....
(물론 객체가 없기때문에 위의 경우는 임시로 바로 받을 변수를 선언해주어야한다) 

※ struts.xml 에서 액션을 선언 할 경우
    인터셉터를 하나도 선언하지 않으면 알아서 구현된 모든 인터셉터를 실행해준다
    하지만.. 단 하나의 인터셉터라도 선언하게 된다면
    인터셉터의 자동 실행은 모두 사라지고 선언된 인터셉터만 실행된다 

    그렇다면 인터셉터는 귀찮게 선언할 필요가 없나 하면 그건 아닌거 같다 

만약 다른 모든 인터셉터가 실행되기 전에 prepare 에서도 전달된 파라메터 값이 필요할 때..


<action name="chkLogin" class="action.CheckLoginAction">
            <interceptor-ref name="params"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="params"/>
            <result>main.jsp</result>
</action>

이렇게 params 를 두번 선언해야 할 경우가 있다. // 스트럿츠2 프로그래밍 - 가메출판사 106 페이지 참고

 이럴때는 자동으로 실행되는 것으론 첫번째 params 가 실행되지 않는다 

※ 그러므로 가능하다면 모든 인터셉터를 선언해주는 것이 가장 좋을 것 같다
    꼭 액션클래스를 안봐도 사용된 인터셉터를 알 수 있게 가독성을 위해서도.

posted by 헤이장