DefaultWorkflowInterceptorÀ¹½ØÆ÷¼ò½é
DefaultWorkflowInterceptorÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚÊ®°ËµÄλÖã¬ÎªdefaultStackÀ¹½ØÆ÷Õ»ÖеÄ×îºóÒ»¸öÀ¹½ØÆ÷£¬ÆäÖ÷Òª¹¦ÄÜÊÇÅжϵ±Ç°ActionµÄÖ´Ðйý³ÌÓÐÎÞ´íÎ󣬰üº¬ActionErrorÓëFieldErrorÁ½ÖÖ£¬ÀýÈçÔÚÇëÇó²ÎÊýÀàÐÍת»»³ö´íºó¾Í»áÌí¼ÓFieldError¡£Èç¹ûÖ´Ðйý³ÌÖÐÓдíÎóÔò»áÌøתµ½Ö¸¶¨µÄResult(ĬÈÏΪinput)¡£
¸ÃÀ¹½ØÆ÷¼Ì³Ð×ÔMethodFilterInterceptorÀ࣬ËùÒÔÒ²¿ÉÒÔÅäÖÃÄÄЩ·½·¨ÒªÓ¦ÓÃÉϸÃÀ¹½ØÆ÷£¬ÄÄЩ·½·¨²»ÒªÓ¦ÓÃÉϸÃÀ¹½ØÆ÷£¬ÏÂÃæÊǸÃÀ¹½ØÆ÷ÔÚdefaultStackÖеÄÅäÖãº
±í1
<interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> |
Ò²¾ÍÊÇ˵Ã÷¶ÔÓÚActionµÄinput,back,cancel,browse·½·¨ÊDz»»áÓ¦ÓÃÉϸÃÀ¹½ØÆ÷µÄ¡£
ÏÂÃæÊǸÃÀ¹½ØÆ÷µÄdoIntercept·½·¨Ô´Â룺
±í2
@Override protected String doIntercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction();//»ñÈ¡µ±Ç°Ö´ÐеÄAction¶ÔÏó //ÅжÏActionÊÇ·ñʵÏÖÁËValidationAware£¬¼Ì³Ð×ÔActionSupportÀàµÄAction¼´ÊµÏÖÁË¸Ã½Ó¿Ú if (action instanceof ValidationAware) { ValidationAware validationAwareAction = (ValidationAware) action;//ǿת //ÅжÏActionÖ´Ðйý³ÌÖÐÓÐÎÞ´íÎó if (validationAwareAction.hasErrors()) { if (LOG.isDebugEnabled()) { LOG.debug("Errors on action " + validationAwareAction + ", returning result name 'input'"); } //inputResultNameÊôÐÔֵΪ"input"£¬ÕâÊÇÔÚ¸ÃÀ¹½ØÆ÷дËÀµÄ String resultName = inputResultName; //ActionÓÐÎÞʵÏÖValidationWorkflowAware½Ó¿Ú if (action instanceof ValidationWorkflowAware) { //Èç¹ûʵÏÖÁËÔòÓÉAcitonµÄgetInputResultName()·½·¨¾ö¶¨resultNameµÄÖµ resultName = ((ValidationWorkflowAware) action).getInputResultName(); } //Åжϵ±Ç°ActionÒªÖ´Ðеķ½·¨ÉÏÊÇ·ñ±êÓÐInputConfig×¢½â InputConfig annotation = action.getClass().getMethod(invocation.getProxy().getMethod(), EMPTY_CLASS_ARRAY).getAnnotation(InputConfig.class); if (annotation != null) { //Èç¹ûInputConfig×¢½âµÄmethodNameÊôÐÔ³¤¶È²»Îª0 if (!annotation.methodName().equals("")) { //»ñÈ¡ActionÖÐÓÉInputConfig×¢½âµÄmethodNameÊôÐÔÖ¸¶¨µÄ·½·¨ Method method = action.getClass().getMethod(annotation.methodName()); //ÔòresultNameÓɸ÷½·¨µÄ·µ»ØÖµ¾ö¶¨ resultName = (String) method.invoke(action); } else {//·ñÔòÓÉInputConfig×¢½âµÄresultNameÊôÐÔÖµ¾ö¶¨ resultName = annotation.resultName(); } } //½«resultNameÖµ·µ»Ø return resultName; } } //Èç¹ûActionûÓÐʵÏÖValidationAware½Ó¿ÚÔòÖ±½ÓÖ´ÐÐActionÖеķ½·¨ return invocation.invoke(); } |
ѧ¹ýstruts2µÄ¿Ï¶¨ÖªµÀ£¬Èç¹ûActionÖ´ÐеĹý³Ì³öÁË´í(µ±È»Ç°ÌáÊÇÌí¼Óµ½ÁËActionError»òFieldErrorÖÐ)¾Í»áתÏòÒ»¸öÃûΪinputµÄResult£¬Æäʵ¾ÍÊÇͨ¹ý¸ÃÀ¹½ØÆ÷ʵÏֵģ¬ÕâÒ²ÊÇΪʲôÕâ¸öResultµÄÃû×ÖΪinputµÄÔÒò¡£
µ±È»Õâ¸öResultµÄÃû×ÖÊÇ¿ÉÒԸĵģ¬·½Ê½¾ÍÓкܶàÁË£º
1.¸ø¸ÃÀ¹½ØÆ÷ÔÚÅäÖÃÎļþÉÏ´«µÝÒ»¸öÃûΪinputResultNameµÄ²ÎÊý£¬ÆäÖµÓÉ×Ô¼º¾ö¶¨
2.ÈÃAcitonʵÏÖValidationWorkflowAware½Ó¿Ú£¬ResultÃû×־ͻáÓÉValidationWorkflowAware½Ó¿ÚÖÐÉùÃ÷µÄgetInputResultName·½·¨¾ö¶¨
3.ÔÚActionÖÐÖ´Ðеķ½·¨ÉÏÃæÌí¼ÓInputConfig×¢½â£¬¸Ã×¢½âÓÐÁ½¸öÊôÐÔ£ºmethodName-->ÓÃÓÚÖ¸¶¨µ±Ç°ActionÖеÄÒ»¸ö·½·¨Ãû³Æ£¬Óø÷½·¨µÄ·µ»ØÖµ×÷ΪResultÃû×Ö£»resultName-->Ö±½ÓÖ¸¶¨ResultÃû×Ö¡£
×îºóµ÷ÓÃinvocation.invoke();Ö´ÐÐActionÖеķ½·¨£¬ÒòΪÔÚdefaultStackÖÐÕâÒѾÊÇ×îºóÒ»¸öÀ¹½ØÆ÷£¬ËùÒÔµ÷ÓõÄÊÇActionÖеķ½·¨ÁË......
END£¡£¡£¡