该用户从未签到
|
struts2 拦截器的配置* }" N- F8 i2 M
<package>3 r% v' j- Y- t, v: G& g; \' Z
<interceptors>
. Q/ c8 e2 _# ]8 P; E9 j <!--声明拦截器,指明拦截器的实现类和名称-->* I* c% R3 u9 s; o5 b) E8 A8 M
<interceptor name="one" class=""> 0 M8 a9 {1 S! T
<interceptor name="two" class="">
- O# b8 m$ ?, y# Y0 V& p. v* l <interceptor name="three" class="">
* ?8 W3 y: P( g+ Z0 g4 Q* n) d' P: j ... .... ....
. P3 ^ H/ V, T5 J( }# ]8 f <!--声明拦截器栈,用一个名称表示一组拦截器--> 8 J' `) ]5 v t+ p- G
<interceptor-stack name="all">
: c* p2 ~8 S+ m8 J <interceptor-ref name="one"/>6 }4 v" z- | g: E8 J! K
<interceptor-ref name="two"/>
0 E; p' {% ^6 H3 F2 ? v2 e% p ... ... ...
" }3 L: y# N4 I </interceptor-stack>
7 S: z# ^& g9 B( U+ Y' K </interceptors>
2 H! m I* d6 m+ J ~ <!--7 Z9 E# W( T4 K. W$ T3 {: w
默认引用的拦截器,如果该包中的Action没有引用拦截器
2 z. C! h. L n% Y& I4 n: I. l 就使用此拦截器 j, B) X$ z/ `7 |) L
--> 6 @$ A9 z% Q3 F
<default-interceptor-ref name=""/>
" C# Z- \. ?# X <action name="" class="">
4 j1 J4 N s8 R7 t0 Z$ S2 t <!--8 ]' x* ?7 E8 y: ^6 q& v# S
在Action中声明拦截器,在调用该Action之前
8 ^; c4 z" f0 ?: M5 _ 会调用这些拦截器,如果声明了拦截器则会覆盖
3 A7 H2 l$ J6 u: H0 D, U0 r7 \ default-interceptor-ref的设置
6 u% F2 g/ R/ w- \& [8 u --> / ?( K4 d1 K# Z
<interceptor-ref name=""/>: \9 o8 c+ r, b7 ]
<interceptor-ref name=""/>
- [6 Z7 G$ }3 P0 W1 i+ M ....* A/ S! b8 F0 W" A. z# }
<result name=""></result>
# B* Q" `7 E7 L% j4 N& ]1 k7 C </action>1 W% l/ a9 N. L: }& X* }' ^- S
</package>
; e( ?# O Y6 b- `( L& R1 ~) ]3. 自定义拦截器/ @6 r1 e$ D2 ]
实现Interceptor,实现其intercept1 t; D8 W' B; L) _. s4 T8 p! B5 i
public class FooInterceptor implements Interceptor { O+ b$ D5 F7 w1 x
... ... ...' ^. ?% O) S9 O/ L
public String intercept(ActionInvocation actionInvocation) throws Exception {
8 }2 w: u; X" D b) @" W3 p: N" @ E // ActionInvocation封装了
0 T) \/ w, a. U6 l+ B$ Q ValueStack vs = actionInvocation.getStack();
( _7 K6 {7 z; T$ ?- @9 y } c1 l+ r" v( V2 {; u
}
$ q! P0 L8 K4 @一般会涉及到:
6 a" s. _6 t; L' L" L1) ValueStack
" R) E$ g3 l d8 C, P1 }ValueStack vs = actionInvocation.getStack();
8 K5 w) U* d1 O" r4 C/ l) Wvs.setValue("ognl", obj);
% q$ _) `6 A, O- Dvs.setValue("emp.name", "java");
# ^( T6 e; t4 }( V; H7 e% l9 f1 q F* \2)Servlet API" Q8 K: n% ] P* `3 ]" n
ServletActionContext.getRequest();
$ X# o+ H, I! `+ n C9 x5 DServletActionContext.getResponse();
' t9 k: H- y D3 b- [& KServletActionContext.getServletContext()
2 u. g0 c7 e5 G% H' s) S/ j3 b* ^3)如何调用后续的Action(其他的Interceptor)1 _/ R$ `7 F( ?- m7 s$ i9 Y+ t7 h7 q" `, ^3 ]
actionInvocation.invoke()
& x, X- Q( b- o& m0 V4 T- fAction调用后,Result调用,然后再调用拦截器后续的部分$ Y" W: E1 `7 d1 S; G1 e! E& H
拦截器无法决定返回何种Result3 }5 A& I/ j7 x
不调用
4 w4 v) ], }9 i( w; X8 Q& `由拦截器的intercept方法的返回值来决定返回何种Result
( `6 `6 ^- r3 R2 @; zactionInvocation.invokeActionOnly()
6 N! ?) }- L; `# R" d只调用Action(不调用Result以及后续拦截器)
- S2 j e/ g) h由拦截器的intercept方法的返回值来决定返回何种Result" W1 s7 x4 r1 I
. v% {9 Q% u" `7 S7 t' i, t( o2 a& O |
|