2013-10-11 2 views
1

은 스트럿 내에서 여러 중첩 된 인터셉터이 조치가 가정 그들의 intercept()에 따라 방법`invocation.invoke()`를 호출하는 다중 Struts2 인터셉터를 갖는 방법?</p> <ul> <li>foo는 <ul> <li>바 인터셉터의 각각을하고</li> </ul></li> </ul></li> </ul> <p> <ul> <li>바즈 :

invocation.invoke()가 호출
String result = invocation.invoke(); 
return result; 

, 이것은 큐의 다음 인터셉터의 intercept() 메소드를 호출하거나 실제로 action.execute() 메소드를 호출?

예 : fooinvocation.invoke() 인 경우이 번호는 bar.intercept()이나 실제로 action.execute()입니까?

만약 후자, 그 다음 제가 (바 인터셉터에서 호출하는 경우) bar.intercept() 또는 baz.intercept()은 즉 그래서 모든 인터셉터가 호출 action.execute() 전에 실행, action.execute() 전에 호출 할 수 있습니다 위해 무엇을 할 수 있는가?

답변

2

action.execute() 메서드가 실행되기 전에 스택의 모든 인터셉터가 호출됩니다. 이 경우 다음이 주문입니다.

  1. foo.intercept()
  2. bar.intercept()
  3. baz.intercept()
  4. action.execute()
+1

그것은 당신이 옳다는 [문서] (http://struts.apache.org/release/2.0.x 보인다 'invoke()'에 대해서 :'다음 단계를 호출합니다 .. 만약 더 많은 인터셉터가 있다면, 다음 인터셉터를 호출 할 것입니다. 인터셉터가 ActionInvocation 처리를 단락시키지 않고 자신의 리턴 코드를 리턴하도록 선택하면 invoke()를 호출하여 다음 인터셉터가 실행될 수있게합니다. 적용 할 인터셉터가 더 이상 없으면 Action이 실행됩니다. ActionProxy getExecuteResult() 메서드가 true를 반환하면 Result도 실행됩니다.' –

+1

@ClickUpvote 먼저 문서를 검사했을 수 있습니다. –

2

Action은 한 번만 실행됩니다. Interceptors은 모두 전처리 (invoke()에 전화하기 전에)과 후속 처리 (invoke()에 전화 한 후)에 대해 한 번 두 번 발사 할 수 있습니다.

따라서, 귀하의 예를 여기에 일어나는 내용은 다음과 같습니다

foo calls invoke() -> calls bar#intercept() 
bar calls invoke() -> calls baz#intercept() 
baz calls invoke() -> calls action#execute() 

action#execute() returns -> baz executes lines after invoke() 
baz#intercept() returns -> bar executes lines after invoke() 
bar#intercept() returns -> foo executes lines after invoke() 

하시기 바랍니다는 Interceptors 서로 직접 호출하지 있습니다. 모든 전화는 Struts 2 프레임 워크를 거쳐야합니다. 그래서 invoke()ActionInvocation 개체에서 호출되어이 전체 흐름을 거의 조율합니다.