该用户从未签到
|
struts2 拦截器的配置
( \( |% R y! T" `& [, w8 Y<package>1 g4 f/ }5 `: n
<interceptors>; o' ]' m( Z/ A7 D6 }0 y4 n- B
<!--声明拦截器,指明拦截器的实现类和名称-->
: W9 F% r8 S4 Z N" k. h <interceptor name="one" class=""> ! F# t- u) j9 |8 K4 w8 t5 f
<interceptor name="two" class="">
: G& U3 e7 f; O. P5 U, D+ J1 u3 j <interceptor name="three" class="">0 g/ y4 F( C* j! D* P8 Z5 ~
... .... .... * i2 _8 q9 p1 f$ n- L# O2 e
<!--声明拦截器栈,用一个名称表示一组拦截器--> - {' Y/ Q& C* ~1 ^
<interceptor-stack name="all">
* r w( ~4 h2 H/ q* @ <interceptor-ref name="one"/>
6 ?2 y9 J2 Q- N0 R <interceptor-ref name="two"/>2 O8 n" P; ~) j3 i" i; `
... ... ...
! p/ z# k; R, a! r; V K </interceptor-stack>, _8 m7 ^+ ]4 m3 w5 ]; c$ M
</interceptors>; r8 \* G/ K6 H8 k% _
<!--
8 d$ b, \' a, s* p7 I$ _ 默认引用的拦截器,如果该包中的Action没有引用拦截器
7 z6 F `5 I& N; c+ I" F1 e" P ` 就使用此拦截器
& L& f- Q& u5 ^ Z( e* z: g" g -->
2 ?& e7 e2 c8 i1 T1 C" C9 u& e <default-interceptor-ref name=""/>
; ]! G0 U* O H- `+ V4 G# R" m% o d <action name="" class="">
( u1 p4 G' ^; G2 e <!--
, ?' H' h2 I' @! ? 在Action中声明拦截器,在调用该Action之前+ h8 N' g( o7 c2 g; x2 y
会调用这些拦截器,如果声明了拦截器则会覆盖8 v9 o9 q" m, q. c, v
default-interceptor-ref的设置
7 u0 w. Z: o' I. l8 s3 n$ U -->
" U0 ^ s8 ]" N7 A2 R <interceptor-ref name=""/>- a3 D/ f1 G3 F% {2 y
<interceptor-ref name=""/>: z/ V& @! r& P5 y+ A/ M0 t
....
! |3 X$ O' W0 q+ J <result name=""></result>4 G, X3 H8 _# g$ K7 a
</action>3 q! p( E( M5 G5 C$ C* x
</package>
$ S0 B v3 l* M: f. L7 T3. 自定义拦截器: \9 q0 N k# u( Z8 ~* Q
实现Interceptor,实现其intercept
# U1 Y: a: R6 rpublic class FooInterceptor implements Interceptor {
8 L1 A) G3 K$ b9 ?) N ... ... ..." @8 h1 C7 w3 o
public String intercept(ActionInvocation actionInvocation) throws Exception {1 U8 H* h3 M' H' }, K* I6 I8 e
// ActionInvocation封装了3 |6 ?5 p6 S+ J& I, n: }) ~+ M
ValueStack vs = actionInvocation.getStack();9 v+ f0 C v+ k& a& P) S
}
0 S% I& x# d* {5 E& @" l# o}
2 z" b4 z& A2 h C5 A一般会涉及到:* s" `4 \% a. C2 h& @+ S2 y
1) ValueStack
I! R% Q6 E0 ?$ z2 F4 h: kValueStack vs = actionInvocation.getStack();3 W2 ?# a+ `; P2 K( V) D
vs.setValue("ognl", obj);* L: ]) d" t/ l9 h. k& z& f
vs.setValue("emp.name", "java");5 [6 [! j( Z6 `; v0 o, M# e
2)Servlet API+ m# d3 [+ T% o
ServletActionContext.getRequest();
& S4 p; s9 Y: N2 sServletActionContext.getResponse();
3 Y& C W$ b+ r% Q8 k. ?& d9 f/ wServletActionContext.getServletContext()
7 d5 M% F1 s4 }. W& s+ l" l3)如何调用后续的Action(其他的Interceptor)
5 O0 M0 x; t+ V5 `actionInvocation.invoke()
1 K4 u5 x/ z4 y* d: q* WAction调用后,Result调用,然后再调用拦截器后续的部分1 Z& H7 y5 e+ b
拦截器无法决定返回何种Result
7 M4 {* U4 K. `" w _. P不调用# b" U! E/ O7 K' t! c* k
由拦截器的intercept方法的返回值来决定返回何种Result
& K0 K6 b {7 j; v) [0 B B7 |actionInvocation.invokeActionOnly()# [* r6 Z/ Y4 L% [) D) d
只调用Action(不调用Result以及后续拦截器)
" L! s: H' r6 v0 F" S( b, i, w由拦截器的intercept方法的返回值来决定返回何种Result9 G/ `* L! K ]
+ x l7 R0 L3 e |
|