该用户从未签到
|
struts2 拦截器的配置
" w, q8 r/ v7 `" m! ?0 j5 a<package>( |$ @) Q m- V% Y
<interceptors>
0 ^( j5 {6 M- | <!--声明拦截器,指明拦截器的实现类和名称-->
0 e8 E: Y0 Y; M; a: p+ V <interceptor name="one" class="">
- m" n- @& F6 x- y. \, C0 ]* s <interceptor name="two" class="">
" L; r3 W% _1 A5 D <interceptor name="three" class="">) l! Y- M% L1 O! Y- S; N/ F$ Z
... .... .... 8 ^$ z9 ?: q8 S
<!--声明拦截器栈,用一个名称表示一组拦截器--> 6 W, y# G/ o' m! L4 _0 R
<interceptor-stack name="all">
4 n, q; N4 i' j <interceptor-ref name="one"/>
5 `2 i) P8 E* @7 Y/ K- w& s <interceptor-ref name="two"/>
2 @. E. ~( f1 A. Y6 U' |. s ... ... ...
8 S8 |2 Z7 }4 n" N& P* b6 h7 ] </interceptor-stack>
+ G; O/ w, o+ H </interceptors>- }( V0 C/ _# ?: C
<!--$ ]' b9 o1 \$ ?
默认引用的拦截器,如果该包中的Action没有引用拦截器# c* a ` c# o' {2 f1 k g5 d, x
就使用此拦截器
: M- b! L9 D1 G' F -->
, s9 E2 ^2 D/ ~+ ^" ^' X <default-interceptor-ref name=""/>
) v1 ^, s1 \ [/ L <action name="" class="">
; r% h3 L' E( g. c6 N \/ T% Z <!--
( E* v0 M2 a; a" z: y% h% E, V; c* ? 在Action中声明拦截器,在调用该Action之前. f9 r$ V B# i" K
会调用这些拦截器,如果声明了拦截器则会覆盖4 ^5 N/ p* W7 _" e. l5 C" ]/ X
default-interceptor-ref的设置: ^" b N# b- `# R* S" r1 u
--> ' Z* v- d% V [5 {# S% Y6 t
<interceptor-ref name=""/>
) g% p1 I A( w& [! d7 R <interceptor-ref name=""/>8 a! j9 N `: w6 n+ U/ F4 o( z: ]
....( M# c2 P _/ |9 W
<result name=""></result>6 N' M; w' I) `
</action>+ L0 t; B/ y2 c: l; f0 M/ y
</package>
' L* j4 t/ Y9 o9 w4 N( f/ j9 D7 F3. 自定义拦截器
- ^ L' g* y0 D3 ]$ v实现Interceptor,实现其intercept6 R* s( F I4 {! E" q
public class FooInterceptor implements Interceptor {% X$ Q1 i3 Q) e; k2 q+ g
... ... ...9 x+ v. f* x( g' S0 x2 ~8 T3 p
public String intercept(ActionInvocation actionInvocation) throws Exception {* u9 ] {: u" N
// ActionInvocation封装了% z! Q7 n* @6 j/ a1 ^0 k8 D6 K
ValueStack vs = actionInvocation.getStack();7 B) x, }; \0 a& h7 G% h* Z
}
0 i% n1 m* O( q7 L8 n4 a+ K3 S}! l/ X7 A. _& X* X0 ?
一般会涉及到:5 @+ ^8 T* ^. U
1) ValueStack
5 u* J, @9 T1 f0 R1 x$ P) |0 DValueStack vs = actionInvocation.getStack();
' c# x) f- m1 n9 xvs.setValue("ognl", obj);
8 n! N! p2 E4 q3 G/ l& M) E* ]vs.setValue("emp.name", "java");
+ G4 ~+ W4 w! s2)Servlet API8 J1 F7 R' O X6 N& A7 p6 a
ServletActionContext.getRequest();
/ A" w9 B- d% G; I" ?& {" X- w/ FServletActionContext.getResponse();7 ^; J- Z/ [( ?: f6 }, H/ N- K
ServletActionContext.getServletContext()
8 P2 i& U3 W; d% ]7 I3)如何调用后续的Action(其他的Interceptor)( i9 L+ T5 h! r% s4 J8 D: x7 r
actionInvocation.invoke()
9 P* F- O7 q# |' q& W& ]0 QAction调用后,Result调用,然后再调用拦截器后续的部分
4 D6 O* ~2 r/ U- C拦截器无法决定返回何种Result
( u' i, C- V( o' J4 c9 E1 Z/ a u不调用
- F9 o( `$ V) R) y由拦截器的intercept方法的返回值来决定返回何种Result& V5 ]0 f5 ?8 o% Z' m. M6 t& m2 c
actionInvocation.invokeActionOnly()
" h! \* ^: J& [; V+ Q+ `6 e I% N/ M只调用Action(不调用Result以及后续拦截器)- |6 v0 u$ y+ s% X
由拦截器的intercept方法的返回值来决定返回何种Result
8 D7 C! l* m4 Y2 W" O. D' y# F) y& O0 C$ r* D$ B+ G
|
|