该用户从未签到
|
1. Action组件: s W/ U6 S5 O2 a/ G) `4 p( m" b
1) 每次请求struts2会创建一个Action对象,不需要线程安全1 a7 b8 ]. ~& k! T X5 c
2) Action对象会被置于当前的ValueStack的CompundRoot的栈顶
4 N2 |+ d' H9 t7 m7 j 可以通过OGNL表达式对当前的Action对象的属性进行访问
* F) {& }! f* m4 Q# @4 M! J3) Action的属性可以用于接收用户的输入;也可以封装对用户的输出: Q8 c2 j+ T3 v$ r0 s3 i2 H" O% b
Action的执行方法:调用业务层,根据输入计算输出
! w9 _7 J" H' S3 V4 o
" w! c' o8 w1 m, i2. Action的配置
4 ~3 q* q# I, c1) 基本配置
/ o3 r6 m8 ]) Y/ ~. G<action name="" class="" method="">& ]# M" C5 T% z' B" ]
</action>$ r3 I; s' F0 Y" D2 I& ^! x Y* r
2) method默认为execute; class默认为ActionSupport! C, v; a$ E$ n/ M
可以更改class的默认值:
9 @ r( |, i7 n4 e2 ~2 _& I- B3 c <default-class-ref class="com.tarena.tts.action.BaseAction" />9 E; g" U* m, Z! y) z4 O
3) 可以实现多个Action配置共用一个Action类,但调用不同的方法' d; l/ x0 ]( B# ~4 H
这样可以控制Action的粒度' R r: [: k5 D6 M* Z. n6 J# h; j
<action name="action1" class="com.tarena.tts.action.FooAction"' v. [9 y% w& a* T
method="one">
5 q6 f% j. q# b& l: ], \</action>
( S3 L# `4 H6 O' ^, y+ [. k( g8 c<action name="action2" class="com.tarena.tts.action.FooAction", m, @8 Y2 L% w# e2 ~5 E
method="two">
8 u3 z0 ] L- r! n4 P; k3 G/ v9 V</action>
. }* [/ i$ C4 h" b! T7 I R4) Action的配置name属性可以使用通配符: "*"2 s, D6 i. J# `8 M% K& r% Q
<action name="*-*-*" class="com.tarena.tts.action.{1}Action" method="{2}">. }2 d. o; ~( j; @' ~5 |
<result name="success">/WEB-INF/jsp/{3}.jsp</result>
. x6 z6 I/ n' t( [</action># Y+ @* X% R; q! }: [' x
+ U0 ~1 F& Z" T* t
5) Action可以被注入属性8 b1 p1 F% X4 z+ J9 Y
<action name="goo" class="com.tarena.tts.action.GooAction">9 z$ [7 |+ _1 ?
<param name="number">120</param>
' n/ H- h k/ |- A</action>6 @ t! q, l6 K. s
: Z7 A) e: R& Z5 z
6) 可以修改默认的后缀
5 {# l0 R( d/ }* g1 ^; |& L: g9 p<constant name="struts.action.extension" value="action" />* g. O' H1 h( j* t% I7 |& c
, K( o9 D2 d& ^! t/ r |1 {
3. Action中如何访问Servlet API4 w) O5 e; x" O5 X7 x
1) & k1 o1 R8 e: o/ D J+ Y
HttpServletRequest request = ServletActionContext.getRequest();1 ~/ M; u8 U& e: p4 Z& u
HttpServletResponse response = ServletActionContext.getResponse();* p* P, R" X& @1 W% U; I
ServletContext application = ServletActionContext.getServletContext();
& O) @6 M* \& W) {/ x0 Y2)% `/ l0 W/ k j
Action实现了Aware接口,Struts2在初始化Action对象的时候会调用; { ^7 D# F$ a W& @
相应的方法,通过参数传递需要的对象(request,response,appliaction...)
* I8 y# _" a' M$ K3 b( T; A$ eAction只需要在该方法中持有该对象,例如:
- `& k) E( G) d: s$ Zpublic class BaseAction implements ServletRequestAware {1 O; H2 H6 q" p: ?' J
protected HttpServletRequest request;; M7 E' ? d8 n# g& D1 z
9 M4 b p- b0 ^8 z, ? /*** h! [" ?0 o& P7 ~ w
* Struts2会调用该方法,传递request对象
+ z7 G8 T5 |! K4 L0 ^ */7 N0 U* y2 O" x3 }- e( }" d
public void setServletRequest(HttpServletRequest request) {
/ i9 ?# t5 D- J( H6 g this.request = request;; `& {0 `& S0 M/ q2 v$ Z! q
}: g5 F) ?* |2 @/ H
}
" Q) m( N6 D9 H; W/ |
/ r0 A8 t+ d8 s- @% ?% k6 l2 }4. Result组件基础7 r4 G: D6 i' V2 N5 A+ v1 `4 ]
Result组件负责将当前ValueStack中的数据以适当的格式输出给用户
) ` D* |" w& D: B# a( W" M8 j6 K不同的的Result针对不同的数据格式; b. t3 u& V T; |: r( j& ~
如果需要特殊的数据格式,可以自定义Result# J9 s/ H2 i: g; n' Z
struts2-json-plugin-2.1.8.1.jar' m5 c `9 @) ?5 ~' p; K2 g/ i! n
8 \+ r$ i5 w: w |
|