struts2À¹½ØÆ÷Ïê½â ÏÂÔر¾ÎÄ

ÓÉ´Ë£¬ÎÒÃǾͿÉÒÔͨ¹ýinvocation.invoke()×÷ΪAction´úÂëÕæÕýµÄÀ¹½Øµã£¬´Ó¶øʵÏÖAOP¡£

InterceptorÀ¹½ØÀàÐÍ

´ÓÉÏÃæµÄ·ÖÎö£¬ÎÒÃÇÖªµÀ£¬Õû¸öÀ¹½ØÆ÷µÄºËÐIJ¿·ÖÊÇinvocation.invoke()Õâ¸öº¯ÊýµÄµ÷ÓÃλÖá£ÊÂʵÉÏ£¬ÎÒÃÇÒ²Õýʽ¸ù¾ÝÕâ¾ä´úÂëµÄµ÷ÓÃλÖã¬À´½øÐÐÀ¹½ØÀàÐ͵ÄÇø·ÖµÄ¡£ÔÚStruts2ÖУ¬InterceptorµÄÀ¹½ØÀàÐÍ£¬·Ö³ÉÒÔÏÂÈýÀࣺ

1. before

beforeÀ¹½Ø£¬ÊÇÖ¸ÔÚÀ¹½ØÆ÷Öж¨ÒåµÄ´úÂ룬ËüÃÇ´æÔÚÓÚinvocation.invoke()´úÂëÖ´ÐÐ֮ǰ¡£ÕâЩ´úÂ룬½«ÒÀÕÕÀ¹½ØÆ÷¶¨ÒåµÄ˳Ðò£¬Ë³ÐòÖ´ÐС£

2. after

afterÀ¹½Ø£¬ÊÇÖ¸ÔÚÀ¹½ØÆ÷Öж¨ÒåµÄ´úÂ룬ËüÃÇ´æÔÚÓÚinvocation.invoke()´úÂëÖ´ÐÐÖ®ºó¡£ÕâЩ´úÂ룬½«Ò»ÕÐÀ¹½ØÆ÷¶¨ÒåµÄ˳Ðò£¬ÄæÐòÖ´ÐС£

3. PreResultListener

ÓеÄʱºò£¬beforeÀ¹½ØºÍafterÀ¹½Ø¶ÔÎÒÃÇÀ´ËµÊDz»¹»µÄ£¬ÒòΪÎÒÃÇÐèÒªÔÚActionÖ´ÐÐÍêÖ®ºó£¬µ«ÊÇ»¹Ã»Óлص½ÊÓͼ²ã֮ǰ£¬×öһЩÊÂÇé¡£Struts2ͬÑùÖ§³ÖÕâÑùµÄÀ¹½Ø£¬ÕâÖÖÀ¹½Ø·½Ê½£¬ÊÇͨ¹ýÔÚÀ¹½ØÆ÷ÖÐ×¢²áÒ»¸öPreResultListenerµÄ½Ó¿ÚÀ´ÊµÏֵġ£ Java´úÂë

1. public interface PreResultListener { 2.

3. /**

4. * This callback method will be called after the Action execution and before the Result execution. 5. *

6. * @param invocation 7. * @param resultCode 8. */

9. void beforeResult(ActionInvocation invocation, String resultCode); 10.}

ÔÚÕâÀÎÒÃÇ¿´µ½£¬Struts2Äܹ»Ö§³ÖÈç´Ë¶àµÄÀ¹½ØÀàÐÍ£¬ÓëÆä±¾ÉíµÄÊý¾Ý½á¹¹ºÍÕûÌåÉè¼ÆÓкܴóµÄ¹Øϵ¡£ÕýÈçÎÒÔÚ֮ǰµÄÎÄÕÂÖÐËùÌáµ½µÄ£º

downpour дµÀ

ÒòΪActionÊÇÒ»¸öÆÕͨµÄJavaÀ࣬¶ø²»ÊÇÒ»¸öServletÀ࣬ÍêÈ«ÍÑÀëÓÚWebÈÝÆ÷£¬ËùÒÔÎÒÃǾÍÄܹ»¸ü¼Ó·½±ãµØ¶ÔControl²ã½øÐкÏÀíµÄ²ã´ÎÉè¼Æ£¬´Ó¶ø³éÏó³öÐí¶à¹«¹²µÄÂß¼­£¬²¢½«ÕâЩÂß¼­ÍÑÀë³öAction¶ÔÏó±¾Éí¡£

ÎÒÃÇ¿ÉÒÔ¿´µ½£¬Struts2¶ÔÓÚÕû¸öÖ´ÐеĻ®·Ö£¬´ÓInterceptorµ½ActionÒ»Ö±µ½Result£¬Ã¿Ò»²ã¶¼Ö°ÔðÃ÷È·¡£²»½öÈç´Ë£¬Struts2»¹ÎªÃ¿Ò»¸ö²ã´Î֮ǰ¶¼ÉèÁ¢ÁËÇ¡ÈçÆä·ÖµÄ²åÈëµã¡£Ê¹µÃÕû¸öAction²ãµÄÀ©Õ¹ÐԵõ½ÁËÊ·ÎÞÇ°ÀýµÄÌáÉý¡£

InterceptorÖ´ÐÐ˳Ðò

InterceptorµÄÖ´ÐÐ˳Ðò»òÐíÊÇÎÒÃÇÔÚÕû¸ö¹ý³ÌÖÐ×î×î¹ØÐĵIJ¿·Ö¡£¸ù¾ÝÉÏÃæËùÌáµ½µÄ¸ÅÄÎÒÃÇʵ¼ÊÉÏÒѾ­Äܹ»´óÖÂÃ÷°×ÁËInterceptorµÄÖ´ÐлúÀí¡£ÎÒÃÇÀ´¿´¿´Struts2µÄReference¶ÔInterceptorÖ´ÐÐ˳ÐòµÄÒ»¸öÐÎÏóµÄÀý×Ó¡£

Èç¹ûÎÒÃÇÓÐÒ»¸öinterceptor-stackµÄ¶¨ÒåÈçÏ£º Xml´úÂë

1.

2. 3. 4. 5. 6.

ÄÇô£¬Õû¸öÖ´ÐеÄ˳Ðò´ó¸ÅÏñÕâÑù£º

ÔÚÕâÀÎÒÉÔ΢¸ÄÁËÒ»ÏÂStruts2µÄReferenceÖеÄÖ´ÐÐ˳ÐòʾÀý£¬Ê¹µÃÕû¸öÖ´ÐÐ˳Ðò¸ü¼ÓÄܹ»±»Àí½â¡£ÎÒÃÇ¿ÉÒÔ¿´µ½£¬µÝ¹éµ÷Óñ£Ö¤Á˸÷ÖÖ¸÷ÑùµÄÀ¹½ØÀàÐ͵ÄÖ´ÐÐÄܹ»¾®¾®ÓÐÌõ¡£

Çë×¢ÒâÔÚÕâÀÿ¸öÀ¹½ØÆ÷ÖеĴúÂëµÄÖ´ÐÐ˳Ðò£¬ÔÚAction֮ǰ£¬À¹½ØÆ÷µÄÖ´ÐÐ˳ÐòÓë¶ÑÕ»Öж¨ÒåµÄÒ»Ö£»¶øÔÚActionºÍResultÖ®ºó£¬À¹½ØÆ÷µÄÖ´ÐÐ˳ÐòÓë¶ÑÕ»Öж¨ÒåµÄ˳ÐòÏà·´¡£

Ô´Âë½âÎö

½ÓÏÂÀ´ÎÒÃǾÍÀ´¿´¿´Ô´Â룬¿´¿´Struts2ÊÇÈçºÎ±£Ö¤À¹½ØÆ÷¡¢ActionÓëResultÈýÕßÖ®¼äµÄÖ´ÐÐ˳ÐòµÄ¡£

֮ǰÎÒÔø¾­Ìáµ½£¬ActionInvocationÊÇStruts2Öеĵ÷¶ÈÆ÷£¬ËùÒÔÊÂʵÉÏ£¬ÕâЩ´úÂëµÄµ÷¶ÈÖ´ÐУ¬ÊÇÔÚActionInvocationµÄʵÏÖÀàÖÐÍê³ÉµÄ£¬ÕâÀÎÒ³éÈ¡ÁËDefaultActionInvocationÖеÄinvoke()·½·¨£¬Ëü½«ÏòÎÒÃÇչʾһÇС£ Java´úÂë

1. /**

2. * @throws ConfigurationException If no result can be found with the returned code 3. */

4. public String invoke() throws Exception { 5. String profileKey = \6. try {

7. UtilTimerStack.push(profileKey); 8.

9. if (executed) {

10. throw new IllegalStateException(\ executed\11. }

12. // ÒÀ´Îµ÷ÓÃÀ¹½ØÆ÷¶ÑÕ»ÖеÄÀ¹½ØÆ÷´úÂëÖ´ÐÐ 13. if (interceptors.hasNext()) {

14. final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();

15. UtilTimerStack.profile(\getName(),

16. new UtilTimerStack.ProfilingBlock() {

17. public String doProfiling() throws Exception {

18. // ½«ActionInvocation×÷Ϊ²ÎÊý£¬µ÷ÓÃinterceptorÖеÄintercept·½·¨Ö´ÐÐ

19. resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); 20. return null; 21. } 22. }); 23. } else {

24. resultCode = invokeActionOnly(); 25. } 26.

27. // this is needed because the result will be executed, then control will return to the Interceptor, which will 28. // return above and flow through again 29. if (!executed) {

30. // Ö´ÐÐPreResultListener

31. if (preResultListeners != null) {

32. for (Iterator iterator = preResultListeners.iterator();

33. iterator.hasNext();) {

34. PreResultListener listener = (PreResultListener) iterator.next(); 35.

36. String _profileKey=\

37. try {

38. UtilTimerStack.push(_profileKey);

39. listener.beforeResult(this, resultCode);

40. }

41. finally {

42. UtilTimerStack.pop(_profileKey); 43. } 44. } 45. } 46.

47. // now execute the result, if we're supposed to 48. // actionÓëinterceptorÖ´ÐÐÍê±Ï£¬Ö´ÐÐResult 49. if (proxy.getExecuteResult()) { 50. executeResult(); 51. } 52.

53. executed = true;