该用户从未签到
|
struts2 拦截器的配置/ }" C& H$ i) z) K
<package>
5 E: s6 @, n9 f; b( u. r <interceptors>
' l1 Z! B" h. ?% B# ~+ K <!--声明拦截器,指明拦截器的实现类和名称-->3 e: W# n2 i# p" O6 r4 i' k
<interceptor name="one" class="">
8 E& e0 u3 m! a <interceptor name="two" class="">
: ]8 X1 E' @" s4 t3 c+ `# t <interceptor name="three" class="">
2 g1 d* h( D6 |; \* Z) _1 R2 a ... .... ....
4 t( A4 n, _( V+ a) \ <!--声明拦截器栈,用一个名称表示一组拦截器--> 0 u- Y1 o5 a1 P' W0 g0 p
<interceptor-stack name="all">4 @$ X/ X/ w e+ Z& T
<interceptor-ref name="one"/>: @# G0 {% e% @
<interceptor-ref name="two"/>
, W% b" x; N7 I0 J7 J2 x ... ... ...( R) V8 |' \) [
</interceptor-stack>9 A: `& Y: L5 k8 K4 f3 e3 b
</interceptors>. P* z+ n+ p2 g$ v3 B, _, S: d
<!--% J, A1 g/ }3 C4 s. _7 P
默认引用的拦截器,如果该包中的Action没有引用拦截器
1 V$ V7 X# n& G7 u8 g( c 就使用此拦截器
) w5 j$ Z0 c2 L+ v6 m --> ' A1 I3 `% h: {" N n7 x8 A
<default-interceptor-ref name=""/>
- i: t9 Y: u0 \) |+ \ {1 k <action name="" class="">1 _8 ]8 K9 B# G* Y
<!--
3 D' M3 S- o6 j9 t- {' S 在Action中声明拦截器,在调用该Action之前6 P- M9 Y1 q9 }3 N1 S
会调用这些拦截器,如果声明了拦截器则会覆盖; Z0 b8 S6 Z+ `) g
default-interceptor-ref的设置* E4 ]2 T" F3 a7 B. I9 L
-->
" I" a8 q: u( w- ?. }0 O <interceptor-ref name=""/>
4 {0 w$ g% m8 |; t <interceptor-ref name=""/>
( n+ {3 o6 \ g' T# n; r+ ` ..... ]+ u% A* h1 m5 X; ~! N8 U
<result name=""></result>
- d- y9 r2 l7 I2 X2 m" } </action>
- T! h6 o* ~; y9 J' l! ?: f</package>2 Q0 L9 q* Z4 y* ~" M) `' ~% H
3. 自定义拦截器
: o( N; W! t8 X* C6 ^! b0 H实现Interceptor,实现其intercept; a3 g+ v6 k6 p) n
public class FooInterceptor implements Interceptor {" P% o7 N1 @2 q% F$ @8 Z
... ... ...+ O5 U5 Z4 s3 T/ W) Q
public String intercept(ActionInvocation actionInvocation) throws Exception {. {7 b0 Z6 e( u3 d/ o1 ?, l" l
// ActionInvocation封装了( ^# `5 d* J1 C# s! U$ c$ ]3 f2 O
ValueStack vs = actionInvocation.getStack(); B, a+ ~0 u/ d4 Q
}2 c5 S, G7 q5 r; T# `4 _' u- D* r' d
}
" t# v, m: M3 Z: z; B一般会涉及到:* ]# q' y8 g+ D# {8 a. i
1) ValueStack
+ C# I8 {/ l3 |$ `6 ?5 MValueStack vs = actionInvocation.getStack();
; m% k- ^: W* L& |3 u' Z. B6 ^vs.setValue("ognl", obj);. l" x/ a/ Q; D8 u- G
vs.setValue("emp.name", "java");7 u7 j4 g5 ?, {0 f8 D/ M
2)Servlet API7 G+ E9 D$ b, n& W6 i
ServletActionContext.getRequest();1 G- g# |: M- `# u
ServletActionContext.getResponse();7 q6 g `: `: \
ServletActionContext.getServletContext()
+ l2 B% [, v8 Z! N$ ?3)如何调用后续的Action(其他的Interceptor)
) W8 ~: b& T3 A; \actionInvocation.invoke(); s1 S& q; F$ `3 R
Action调用后,Result调用,然后再调用拦截器后续的部分6 F5 S8 n7 f P0 M% C# S
拦截器无法决定返回何种Result
$ H! e, N1 |3 Q" q: R! X4 Z2 A不调用
7 w z" q( \1 Q0 U% ^% [由拦截器的intercept方法的返回值来决定返回何种Result5 W* ~' e. K% m0 [
actionInvocation.invokeActionOnly()
- ~6 i6 B3 H% S" W3 A6 W: h0 ?& U只调用Action(不调用Result以及后续拦截器) J$ G2 k" B; E$ G# \6 s
由拦截器的intercept方法的返回值来决定返回何种Result
8 r7 w) a& d" O4 T* _6 m) v C3 o( J8 \1 d
|
|