|
该用户从未签到
|
struts2 拦截器的配置
, D0 e3 K6 V4 U/ u1 I2 K7 Q<package>, x9 }6 i; U, P4 s/ Y5 P" _
<interceptors>9 c* k* t) o, }6 V0 [6 {
<!--声明拦截器,指明拦截器的实现类和名称-->7 r) u- g! n. [! i/ B
<interceptor name="one" class=""> 0 k5 \- L4 H2 m0 { Y _
<interceptor name="two" class=""> & r7 {, Q' r: h9 r, }! h. _) H
<interceptor name="three" class="">
% T# [; [( B' e" M& R7 M* D- K( L ... .... ....
2 x4 U H* j z0 \ <!--声明拦截器栈,用一个名称表示一组拦截器-->
8 c4 E9 K& b) Y <interceptor-stack name="all">4 O, y7 }* W8 [
<interceptor-ref name="one"/>
3 m5 F T0 M$ {+ s7 f <interceptor-ref name="two"/> ^- E' a) c5 T7 m
... ... ...5 R w* t* Z1 R3 }* O: h$ s
</interceptor-stack>/ \/ b' k4 f) n& a; R5 n1 e x! A
</interceptors>8 o: Q5 b- x! I' U7 s! h3 r' M
<!--
( `( N2 k4 R- B/ |; ?- I3 t 默认引用的拦截器,如果该包中的Action没有引用拦截器$ X) K" _) j, N7 ?, V# q* T
就使用此拦截器 # k; A, w6 \; G2 V( ^9 Z8 Q/ G
-->
" o7 Q5 N6 r( F2 v) I% m! S <default-interceptor-ref name=""/>
* G' W2 A4 a: N. G; X7 }) q- N <action name="" class="">3 f" ^( N. W9 A4 t
<!--
, H: }, ?3 ?* A* F& C3 b4 [ 在Action中声明拦截器,在调用该Action之前/ E) V- M% j* l- S6 E, v
会调用这些拦截器,如果声明了拦截器则会覆盖
! y* F" C/ m7 m; t) Q/ q$ { default-interceptor-ref的设置2 b% x0 O3 Z8 N+ v$ c9 ^2 g+ D0 }% f
--> X7 r/ ~) m* V# C* Z
<interceptor-ref name=""/>
% T; D- m, w' s' ]# @6 g6 S1 q <interceptor-ref name=""/>8 j1 Y d& G3 i' }7 s, B9 k
....
. @' K; t1 n1 t8 K' F8 S <result name=""></result>; d4 C3 ]9 R7 n; p/ q
</action>8 t) V3 \$ s( h1 J
</package>$ s- i$ Q' b: ]4 m3 K5 F
3. 自定义拦截器3 r7 s! L+ ?! L9 `2 R0 j
实现Interceptor,实现其intercept2 Y- z" N( O+ [( e+ [* x4 e
public class FooInterceptor implements Interceptor {
; h1 r& s& L5 F; B+ w3 m7 Y, A/ S ... ... ...
. B4 C* K. g) ^7 v public String intercept(ActionInvocation actionInvocation) throws Exception {
# u1 W+ j! a+ t( [$ e4 k // ActionInvocation封装了
! k% i8 N) N G) j. u ValueStack vs = actionInvocation.getStack();7 O+ ~. u& o0 u* q* L4 H
}
# j+ }3 [. h0 B}
- z: h: P' t1 u2 U Y! F: C一般会涉及到:# P3 v$ t! t5 z
1) ValueStack' U8 T6 w A1 @" L( @; A- @
ValueStack vs = actionInvocation.getStack();) I# M. S/ G9 b7 @: |1 ^
vs.setValue("ognl", obj);
/ i2 I0 t- H3 dvs.setValue("emp.name", "java");
+ H; z, ^) g+ \: ]0 i( @2)Servlet API, H X+ B# ?5 g; J: M
ServletActionContext.getRequest();4 k5 t8 i: l4 C4 g0 v
ServletActionContext.getResponse();
" O& b6 _$ T$ Y( H/ ]+ V- PServletActionContext.getServletContext()0 H; ^" X% H' o5 N; ?4 w
3)如何调用后续的Action(其他的Interceptor)
% D+ z3 y( _. o; l8 u9 v- PactionInvocation.invoke()
& [# o% r9 A- a2 DAction调用后,Result调用,然后再调用拦截器后续的部分
- W" w4 A Y; O; Z5 L! o拦截器无法决定返回何种Result
8 D5 o* k# y y7 \+ R, v不调用
+ c/ y+ k6 {2 \2 o9 U8 G由拦截器的intercept方法的返回值来决定返回何种Result
* S& o+ }9 D3 YactionInvocation.invokeActionOnly()
6 e5 \$ n6 H0 \/ u. T2 A* P2 ?只调用Action(不调用Result以及后续拦截器)" V/ o0 n3 V8 d. v* |8 {% o1 d/ {
由拦截器的intercept方法的返回值来决定返回何种Result
$ w9 T# F. y/ U0 a! j( u
5 o- s7 E( M- j: Q4 L6 g8 q |
|