struts2的配置文件是 struts.xml,在这个配置文件里面可以使用通配符。其中的好处就是,大大减少了配置文件的内容,当然,相应付出的代价是可读性比较差。 struts2通配符的使用方法:形式一:调用相同Action中的不同方法6 X# G8 x8 J4 w6 z# U
<action name="*Action" method="{1}">
9 Q5 a2 q# b* g7 i<result name="input">/login.jsp</result>
' ?: a9 d4 I1 @. f* `6 z<result name="error">/error.jsp</result>2 g- D5 n- x# s9 n$ K0 }: G
<result name="success">/welcome.jsp</result>
( \1 D% f9 H$ s+ o, w; `</action>
% }/ B b8 M- ]5 Y其中表达式{1}的值--->name属性值中第一个*的值。! m0 N, T* Z* F. I6 z, P1 X
如果用户请求的URL为loginAction.action,则调用LoginRegistAction中的login方法;# V2 B1 s9 v% Z- s' I! o
如果用户请求的URL为registerAction.action,则调用LoginRegistAction中的register方法;$ x& R* b& ~) u$ O2 ^; l
形式二:通过匹配,调用不同的Action的execute方法& u: y% x7 ~9 d; ]
<action name="*Action">, S: M4 t" X$ _0 N& N8 q
<result name="input">/login.jsp</result>
3 Z$ R; B! n' O6 M I& P<result name="error">/error.jsp</result>
/ W' O1 K" `/ A, ]2 L: S# s' m<result name="success">/welcome.jsp</result>
3 j1 e4 j5 y4 s$ @ ^, j3 ~</action>3 X- F( N8 L% H& [, X. \4 J) ?
上面没有出现method属性,故默认调用对应的execute方法- b5 G( }7 S' P& l
如果用户请求的URL为LoginAction.action,则调用LoginAction中的execute方法;0 h" V& l4 m" L ?
如果用户请求的URL为RegisterAction.action,则调用RegisterAction中的execute方法;
) ? R/ }2 P T- ^形式三:动态结果/ h( n' u O2 T+ Z3 E% i+ m/ E7 E* o, I
<action name="crud_*" method="{1}">
6 t9 U" _5 y3 M' X<result name="input">/input.jsp</result>5 p' {1 I- c0 K1 y1 g# }7 b/ p
<result>/{1}.jsp</result>
* T5 J0 Z2 m4 A$ s$ c6 r. c/ c</action>! w( ~: A8 d, U# ^: e7 O
当处理结果是success时,# A8 k% J- E# k4 X3 U
如果crud_create.action,则会执行CrudAction中的create方法,并且跳转到/create.jsp;
* E w. s, O1 w5 Y) x如果crud_delete.action,则会执行CrudAction中的delete方法,并且跳转到/delete.jsp; struts2通配符注意事项:最后需要注意的一点是匹配的优先权。5 |7 V% k( e; C* e
(1)如果struts.xml里面有对应的action name。就算它有其他通配符匹配的,都优先对应完全相同的.
5 } O8 ~% \6 |! s. o* `比如,有一个 action name 是 "user_add" 还有一个是 "user_*" .现在,来一个请求是 "user_add.action " .那么,它会优先匹配"user_add " 。其实,这个很好理解。7 J( c* D' t! e
(2)如果一个action name对应于两个带通配符的action name 那么,需要看这个配置谁在前面。它匹配写在前面的1 J7 q! ~; Q- T# w( ^5 j
比如 有一个 action name 是 "*_*" 还有一个是 "user_*" 。现在,来一个请求是 "user_add.action "。那么,它会优先匹配写在前面的。
3 M: {5 o Q. B任何带"*"的action name 优先权都是一样的。不是说带一个"*"的优先权就比带两个"*" 的高。 % i/ } ~6 p6 C$ h5 E) N: F# z
|