ModelDrivenInterceptorÀ¹½ØÆ÷¼ò½é
¸ÃÀ¹½ØÆ÷´¦ÓÚdefaultStackÖеĵھŵÄλÖã¬ÔÚScopedModelDrivenInterceptorÀ¹½ØÆ÷Ö®ºó£¬ÒªÊ¹¸ÃÀ¹½ØÆ÷ÓÐЧµÄ»°£¬Action±ØÐëʵÏÖModelDriven½Ó¿Ú£¬¸Ã½Ó¿Ú¾ÍÒ»¸ö·½·¨£ºgetModel()£¬ModelDrivenInterceptorÀ¹½ØÆ÷Ö÷Òª×öµÄʾÍÊǵ÷ÓÃActionµÄgetModel()·½·¨È»ºó°Ñ·µ»ØµÄmodelѹÈëÖµÕ»(Èç¹û²»Îªnull)¡£
Èç¹ûActionʵÏÖÁËScopedModelDriven½Ó¿ÚÒ²¾ÍʵÏÖÁËModelDriven½Ó¿Ú£¬ÒòΪScopedModelDrivenInterceptorÔÚÖ´ÐеĹý³Ì¿Ï¶¨»á·µ»ØÒ»¸ömodel¶ÔÏóÔÙµ÷ÓÃActionµÄsetModel(model)·½·¨£¬Èç¹ûAction¶Ômodel½øÐÐÁ˽ÓÊÕ£¬ÄÇôÔÚÖ´Ðе½ModelDrivenInterceptorÀ¹½ØÆ÷µÄʱºò£¬ActionµÄgetModel()·½·¨·µ»ØµÄ¾ÍÊÇScopedModelDrivenInterceptorÀ¹½ØÆ÷ÉèÖýøÈ¥µÄÖµ£¬ÒѾ²»ÎªnullÁË£¬ËùÒÔ¸Ãmodel×ÔÈ»¾Í»áѹÈëÖµÕ»¡£ÏÂÃæÊǸÃÀ¹½ØÆ÷intercept·½·¨Ô´Â룺
±í1
@Override public String intercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction();//»ñÈ¡µ±Ç°ÕýÔÚÖ´ÐеÄAction //Èç¹ûActionʵÏÖÁËModelDriven½Ó¿Ú if (action instanceof ModelDriven) { ModelDriven modelDriven = (ModelDriven) action; ValueStack stack = invocation.getStack(); Object model = modelDriven.getModel();//ͨ¹ýgetModel·½·¨»ñÈ¡model if (model != null) {//Èç¹ûmodel²»ÎªnullÔò°ÑmodelѹÈëÖµÕ» stack.push(model); } if (refreshModelBeforeResult) {//ÔÚÖ´ÐÐResult֮ǰÊÇ·ñÒª¸üÐÂmodel¶ÔÏó£¬Ä¬ÈÏΪfalse invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model)); } } return invocation.invoke();//µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ } |
¸Ã·½·¨Âß¼ºÜ¼òµ¥£¬ÕýÈçÇ°Ëù˵µÄÒ»Ñù£¬¾ÍÊÇ°ÑgetModel·½·¨·µ»ØµÄ½á¹ûѹÈëÖµÕ»¶øÒÑ£¬ÎÒÃÇÒ»°ãʵÏÖÕâ¸ö½Ó¿ÚÊÇÀûÓÃѹÈëÖµÕ»µÄmodel¶ÔÏó½ÓÊÕ´ÓÒ³ÃæÌá½»¹ýÀ´µÄÊý¾Ý£¬ÓкܶàʱºòÎÒÃÇÊÇÔÚActionÖÐдÊôÐÔÀ´½ÓÊÕ²ÎÊýµÄ£¬ÒòΪActionÒ²ÊÇÔÚÖµÕ»ÖУ¬¶østruts2ÔÚ¸³Öµ²ÎÊýµÄʱºòÊÇÔÚÖµÕ»´ÓÕ»¶¥ÍùÕ»µ×Ñ°ÕÒÓÐÏàÓ¦setter·½·¨µÄ¶ÔÏ󣬶øÕâʱmodelѹÈëÁËÖµÕ»£¬ËüÊÇ´¦ÓÚÕ»¶¥µÄ£¬ËùÒÔ´ÓÒ³ÃæÌá½»¹ýÀ´µÄ²ÎÊýÒ²¾Í±»model¶ÔÏó½ÓÊÕÁË¡£ÕâÖÖ·½Ê½ÄØmodel¶ÔÏóÓеãÏñstruts1.XÀïÃæformbean¶ÔÏóµÄ¹¦ÄÜ¡£
ModelDrivenInterceptorÀ¹½ØÆ÷Öл¹ÓиöÃûΪrefreshModelBeforeResultµÄÊôÐÔ£¬ÓÃÓÚÉèÖÃÔÚÖ´ÐÐResult֮ǰÊÇ·ñÒª¸üÐÂmodel¶ÔÏó£¬Ä¬ÈÏÖµÊÇfalseÒ²¾Í²»»áÈ¥¸üУ¬ÎÒÃÇÓпÉÄÜÔÚActionµÄÖ´Ðйý³Ì½«modelÕâ¸öÒýÓñäÁ¿Ö¸ÏòÁËÁíÍâÒ»¸ö¶ÔÏó£¬Èç¹ûÄã°ÑrefreshModelBeforeResultÉèÖÃÁËΪtrue£¬ÄÇôÔÚResultÖ´ÐÐ֮ǰPreResultListener¾Í»áÓÃÕâ¸öеĶÔÏó½«ÖµÕ»ÖеĶÔÏóÌæ»»£¬´ïµ½¸üÐÂЧ¹û¡£
PreResultListenerÊÇÒ»ÖÖ¼àÌýÆ÷ÓÉActionInvocation¶ÔÏó½øÐÐ×¢²á£¬ÔÚResultÖ´ÐÐ֮ǰ»áÖ´ÐиÃÖÖ¼àÌýÆ÷µÄbeforeResult·½·¨¡£ËäÈ»ÔÚdefaultStackÖУ¬struts2ÊDz»»áÔÚActionInvocationÖÐ×¢²áÕâ¸öRefreshModelBeforeResult¼àÌýÆ÷µÄ£¬ÎÒÃÇ»¹ÊÇÈ¥¼òµ¥¿´Ò»Ï£º
±í2
protectedstaticclass RefreshModelBeforeResult implements PreResultListener { private Object originalModel = null; protected ModelDriven action;
public RefreshModelBeforeResult(ModelDriven action, Object model) { this.originalModel = model; this.action = action; }
publicvoid beforeResult(ActionInvocation invocation, String resultCode) { ValueStack stack = invocation.getStack();//»ñÈ¡ÖµÕ» CompoundRoot root = stack.getRoot();//»ñÈ¡ÖµÕ»µÄroot¶ÔÏó
boolean needsRefresh = true; Object newModel = action.getModel();//´ÓActionÖлñȡеÄmodel¶ÔÏó
// Check to see if the new model instance is already on the stack for (Object item : root) { if (item.equals(newModel)) {//Èç¹ûеÄmodel¶ÔÏóÓë¾ÉµÄÏàͬÔò²»Ë¢Ð needsRefresh = false; } }
// Add the new model on the stack if (needsRefresh) {//Èç¹ûҪˢÐÂ
// Clear off the old model instance if (originalModel != null) { root.remove(originalModel);//ÏÈÒƳý¾Émodel } if (newModel != null) { stack.push(newModel);//½«ÐÂmodel¶ÔÏóѹÈëÖµÕ» } } } } |
RefreshModelBeforeResultʵÏÖÁËPreResultListener½Ó¿Ú£¬ÊÇModelDrivenInterceptorµÄÒ»¸ö¾²Ì¬ÄÚ²¿À࣬ÔÚÖ´ÐÐResult֮ǰִÐÐÆäbeforeResult·½·¨
ÈçºÎ½øÐÐˢеĺܼòµ¥ÏàÐÅ´ó¼Ò¶¼¿´µÃ¶®¡£
intercept·½·¨×îºóÒ»¾äinvocation.invoke();µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷......