锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鎷︽埅鍣ㄨ窡filter鐨勬蹇墊槸闈炲父綾諱技鐨?鎷︽埅鍣ㄥ彧鑳芥嫤鎴狝ction鐨?鑰宖ilter鏄彲浠ヨ繃婊ゆ墍鏈夌殑涓滆タ鐨?An interceptor is a stateless class that follows the interceptor pattern, as found inFilter
and in AOP languages.
Interceptors must be stateless(鏃犵姸鎬佺殑) and not assume that a new instance will be created for each request or Action鏄崟瀹炰緥鐨?
鎷︽埅鍣ㄧ殑緙栧啓鏈変袱姝?涓涓槸緙栧啓浠g爜,絎簩涓槸緙栧啓閰嶇疆鏂囦歡.
鑷畾涔夌殑鎷︽埅鍣ㄨ瀹炵幇Interceptor鎺ュ彛ActionInvocation鐨刬nvoke鏂規硶灝辯被浼間簬filter鐨刣ofilter
閰嶇疆鏂囦歡涓敤<interceptors>涓繘琛岄厤緗?娉ㄦ剰鎷︽埅鍣ㄧ殑init鏂規硶涔熸槸鍦ㄦ湇鍔″櫒涓鍚姩鐨勬椂鍊欏氨璋冪敤鐨?璺焒ilter涓鏍風殑.鏁翠釜struts閮芥槸闈犺繖浜涙嫤鎴櫒鎵涓茶仈璧鋒潵鐨?鎴戜滑騫舵病鏈夊湪action閲岄潰閰嶇疆涔熸病闂鍟?鍥犱負浠栨湁涓粯璁ょ殑鎷︽埅鍣ㄦ爤鍢?鏄氳繃 <default-interceptor-ref name="defaultStack"/>鏉ユ寚瀹氱殑.
娉ㄦ剰:涓鏃﹀畾涔変簡鑷繁鐨勬嫤鎴櫒錛屽皢鍏墮厤緗埌action涓婂悗錛屾垜浠渶瑕佸湪action鐨勬渶鍚庡姞涓婇粯璁ょ殑鎷︽埅鍣ㄦ爤錛歞efaultStack銆傚洜涓鴻嚜宸辯殑浼氳鐩栭粯璁ょ殑.
鎷︽埅鍣ㄤ篃鍙互璺焒ilter涓鏍烽厤緗畃aram,鐒跺悗鍦ㄧ▼搴忛噷闈㈠彲浠ユ妸瀹冭鍑烘潵.鍦ㄧ▼搴忎腑瀹冪敤setter鏂規硶set榪涘幓鐨?
濡?
<interceptor name="theInterceptor1" class="cn.wenping.interceptor.TheInterceptor1">
<param name="test">wenp</param>
</interceptor>
鎷︽埅鍣ㄥ畾涔夊涓?
public class TheInterceptor1 implements Interceptor {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
.............
..............
}
瀹氫箟鎷︽埅鍣ㄦ椂鍙互鐩存帴緇ф壙AbstractInterceptor鎶借薄綾?/span>錛堣綾誨疄鐜頒簡Interceptor鎺ュ彛錛屽茍涓斿init鍜宒estroy鏂規硶榪涜浜嗙┖瀹炵幇錛夛紝鐒跺悗瀹炵幇鍏舵娊璞℃柟娉昳ntercept鍗沖彲,瀹為檯涓婂氨鏄竴涓傞厤鍣?br />
娉ㄦ剰:浠ヤ笂璇寸殑榪囨護Action瀹為檯涓婃槸榪囨護浠栫殑execute鏂規硶,濡傛灉瑕佸鎸囧畾鐨勬柟娉曡繘琛屾嫤鎴敤:MethodFilterInterceptor,姝ゅ嵆鏂規硶榪囨護鎷︽埅鍣紙鍙互瀵規寚瀹氭柟娉曡繘琛屾嫤鎴殑鎷︽埅鍣級銆?/p>
閲囩敤鐨勬槸涓嬮潰涓ょparam榪涜閰嶇疆
<interceptor-ref name="theInterceptor3">
<param name="includeMethods">myExecute</param>
</interceptor-ref>
娉ㄦ剰:鍦ㄦ柟娉曡繃婊ゆ嫤鎴櫒涓紝濡傛灉鏃㈡病鏈夋寚瀹歩ncludeMethods鍙傛暟錛屼篃娌℃湁鎸囧畾execludeMethods鍙傛暟錛岄偅涔堟墍鏈夌殑鏂規硶閮戒細琚嫤鎴紝涔熷氨鏄鎵鏈夌殑鏂規硶閮借璁や負鏄痠ncludeMethods鐨勶紱濡傛灉浠呬粎鎸囧畾浜唅ncludeMethods錛岄偅涔堝彧浼氭嫤鎴猧ncludeMethods涓殑鏂規硶錛屾病鏈夊寘鍚湪includeMethods涓殑鏂規硶灝變笉浼氳鎷︽埅銆?/p>
鍏充簬鎷︽埅鍣ㄤ腑鐨勭洃鍚櫒:
ActionInvocation涓殑鏂規硶:addPreResultListener
PreResultListeners may be registered with anActionInvocation
to get a callback after theAction
has been executed but before theResult
is executed.
浠栬搗鐨勪綔鐢ㄥ氨鏄?濡傛灉浣犳兂鍦ˋction鐨勬柟娉曟墽琛屽畬姣曚箣鍚庡湪緇撴灉榪斿洖涔嬪墠鎯寵鍋氱偣涓滆タ鐨勮瘽灝卞彲浠ョ敤瀹冧滑.