AnnotationValidationInterceptorÀ¹½ØÆ÷¼ò½é
AnnotationValidationInterceptorÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚÊ®ÆßµÄλÖã¬Ö÷ÒªÊÇÓÃÓÚÊý¾ÝУÑéµÄ£¬¸ÃÀ¹½ØÆ÷¼Ì³Ð×ÔValidationInterceptorÀ¹½ØÆ÷Ôö¼ÓÁËÔÚ·½·¨ÉÏʹÓÃ×¢½âÈ¡ÏûУÑ鹦ÄÜ¡£ValidationInterceptorÓּ̳Ð×ÔMethodFilterInterceptor¡£Ç°Ãæ¼Ì³Ð×ÔMethodFilterInterceptorµÄÀ¹½ØÆ÷Öж¼Ã»Óн²MethodFilterInterceptor£¬ÔÚAnnotationValidationInterceptorÅäÖÃÖд«µÝÁËÒ»¸öÃûΪexcludeMethodsµÄ²ÎÊý£¬Õâ¸ö²ÎÊý¾ÍÊÇÌá½»µ½MethodFilterInterceptorÖеģ¬ÓÃÓÚÖ¸¶¨ÄÄЩ·½·¨ÊDz»ÐèÒª½øÐÐУÑéµÄ¡£ËùÒÔÕâÀïÏȽ²½âMethodFilterInterceptorÀ¹½ØÆ÷£¬ÏÂÃæÊÇMethodFilterInterceptorµÄinterceptÔ´Â룺
±í1
@Override public String intercept(ActionInvocation invocation) throws Exception { if (applyInterceptor(invocation)) { return doIntercept(invocation); } return invocation.invoke(); } |
ÕâÀïÔÚÖ´ÐÐinvocation.invoke();֮ǰµ÷ÓÃÁËapplyInterceptorÅжÏÊÇ·ñÒªÓ¦ÓÃÉϸÃÀ¹½ØÆ÷£¬ÏÂÃæ¿´Ò»ÏÂapplyInterceptor·½·¨£º
±í2
protectedboolean applyInterceptor(ActionInvocation invocation) { String method = invocation.getProxy().getMethod(); //ÕæÕýÅжϵķ½·¨ÊÇMethodFilterInterceptorUtil.applyMethod·½·¨£¬°ÑÅųýµÄ·½·¨¼¯ºÏÓë°üº¬µÄ·½·¨¼¯ºÏÓëActionÒªÖ´Ðеķ½·¨Ãû´«Èë //¸Ã·½·¨°Ñ×Ö·û´®×ª³ÉÁËÕýÔò±í´ïʽ¶Ô¸Ã·½·¨½øÐÐÆ¥Å䣬Âß¼²»ÄÑ£¬µ«ÅжϴúÂë±È½Ï¶à£¬ËùÒÔ½²µ½Õâ°É... boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method); if (log.isDebugEnabled()) { if (!applyMethod) { log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list."); } } return applyMethod; } |
ËùÒÔËùÓм̳Ð×ÔMethodFilterInterceptorµÄÀ¹½ØÆ÷¶¼¿ÉÒÔÉèÖÃexcludeMethodsÓëincludeMethods²ÎÊýÓÃÓÚÖ¸¶¨ÄÄЩ·½·¨ÒªÓ¦ÓÃÉϸÃÀ¹½ØÆ÷£¬ÄÄЩ·½·¨²»ÐèÒªÓ¦ÓÃÉϸÃÀ¹½ØÆ÷£¬¶ÔÓÚAnnotationValidationInterceptor¾ÍÊÇÄÄЩ·½·¨Òª½øÐÐУÑéÓëÄÄ·½·¨²»ÐèÒª½øÐÐУÑé¡£AnnotationValidationInterceptorÔÚdefaultStackÖеÄÅäÖÃΪ£º
±í3
<interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> |
¼´Åųýinput,back,cancel,browseÕâËĸö·½·¨Í⣬ÆäËüÖ´ÐеÄAction·½·¨¶¼Òª½øÐÐУÑé¡£
ÏÖÔÚ¼ÙÉèÒª½øÐÐУÑ飬ËùÒÔ»áÖ´ÐÐAnnotationValidationInterceptorµÄdoIntercept·½·¨£¬ÏÂÃæÊǸ÷½·¨Ô´Â룺
±í4
protected String doIntercept(ActionInvocation invocation) throws Exception { //»ñÈ¡µ±Ç°Ö´ÐеÄAction Object action = invocation.getAction(); if (action != null) {//Èç¹ûAction²»Îªnull
//»ñÈ¡ActionÒªÖ´Ðеķ½·¨ Method method = getActionMethod(action.getClass(), invocation.getProxy().getMethod()); //»ñÈ¡ActionÖмÓÁËSkipValidation×¢½âµÄ·½·¨¼¯ºÏ Collection<Method> annotatedMethods = AnnotationUtils.getAnnotatedMethods(action.getClass(), SkipValidation.class); if (annotatedMethods.contains(method))
//Èç¹ûµ±Ç°Ö´Ðеķ½·¨ÓÐSkipValidation×¢½âÔò²»½øÐÐУÑ飬µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ return invocation.invoke();
//¼ì²âÊÇ·ñÓи²¸Ç¸¸Àà±êÓÐSkipValidation×¢½âµÄ·½·¨ Class clazz = action.getClass().getSuperclass();//»ñÈ¡¸¸Àà×Ö½ÚÂë while (clazz != null) { annotatedMethods = AnnotationUtils.getAnnotatedMethods(clazz, SkipValidation.class);//»ñÈ¡¸¸Àà±êÓÐSkipValidation×¢½âµÄ·½·¨¼¯ºÏ if (annotatedMethods != null) {//Èç¹û·½·¨²»Îªnull //Èç¹ûµ±Ç°ÒªÖ´Ðеķ½·¨ÊǸ²¸ÇÁ˸¸ÀàµÄ·½·¨£¬¶ø¸¸Àà·½·¨±êÓÐSkipValidation×¢½â£¬Ôòµ±Ç°·½·¨Ò²²»½øÐÐУÑé for (Method annotatedMethod : annotatedMethods) { if (annotatedMethod.getName().equals(method.getName()) && Arrays.equals(annotatedMethod.getParameterTypes(), method.getParameterTypes()) && Arrays.equals(annotatedMethod.getExceptionTypes(), method.getExceptionTypes())) return invocation.invoke();//µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ } } clazz = clazz.getSuperclass();//»ñÈ¡¸¸Àà×Ö½ÚÂë } } //Èç¹ûÒª½øÐÐУÑ飬¼ÌÐøµ÷Óø¸ÀàµÄdoIntercept·½·¨ return super.doIntercept(invocation); } |
´ÓÉÏÃæ¿ÉÒÔ¿´µ½Èç¹ûµ±Ç°ActionÖ´Ðеķ½·¨ÉÏÃæÈç¹û±ê×¢ÓÐSkipValidation×¢½â»òÕßÆ串¸ÇµÄ·½·¨Éϱê×¢ÓÐSkipValidation×¢½â¾Í²»»á¶Ô¸Ã·½·¨½øÐÐУÑ飬ִÐÐÍê³Éºó»¹µ÷ÓÃÁ˸¸ÀàValidationInterceptorµÄdoIntercept·½·¨£¬ÏÂÃæ¸Ã·½·¨Ô´Â룺
±í5
@Override protected String doIntercept(ActionInvocation invocation) throws Exception { doBeforeInvocation(invocation);//µ÷ÓÃdoBeforeInvocation·½·¨ return invocation.invoke();//µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ } doBeforeInvocation(invocation)·½·¨Ô´Â룺 protectedvoid doBeforeInvocation(ActionInvocation invocation) throws Exception { Object action = invocation.getAction();//»ñÈ¡µ±Ç°Ö´ÐеÄAction ActionProxy proxy = invocation.getProxy();//»ñÈ¡ActionProxy¶ÔÏó
//the action name has to be from the url, otherwise validators that use aliases, like //MyActio-someaction-validator.xml will not be found, see WW-3194 String context = proxy.getActionName();//»ñÈ¡ActionÃû³Æ String method = proxy.getMethod();//»ñÈ¡Ö´ÐÐActionµÄ·½·¨Ãû³Æ //Ê¡ÂÔ... //declarativeĬÈÏΪtrue if (declarative) { if (validateAnnotatedMethodOnly) {//validateAnnotatedMethodOnlyĬÈÏΪfalse actionValidatorManager.validate(action, context, method); } else { actionValidatorManager.validate(action, context);//ËùÒÔÖ´ÐÐÕâÀï } } //Èç¹ûActionʵÏÖÁËValidateable½Ó¿Ú£¬ActionSupportʵÏÖÁËValidateable½Ó¿Ú if (action instanceof Validateable && programmatic) {//programmaticĬÈÏΪtrue Exception exception = null; //ǿת Validateable validateable = (Validateable) action; if (LOG.isDebugEnabled()) { LOG.debug("Invoking validate() on action "+validateable); } try {//µ÷ÓÃÓÐvalidate£¬validateDoǰ׺µÄ·½·¨ PrefixMethodInvocationUtil.invokePrefixMethod( invocation, new String[] { VALIDATE_PREFIX, ALT_VALIDATE_PREFIX }); } catch(Exception e) { // If any exception occurred while doing reflection, we want // validate() to be executed LOG.warn("an exception occured while executing the prefix method", e); exception = e; } //alwaysInvokeva lidateĬÈÏΪtrue£¬×ÜÊǵ÷ÓÃActionµÄvalidate·½·¨ if (alwaysInvokeva lidate) { validateable.validate(); } if (exception != null) { // rethrow if something is wrong while doing validateXXX / validateDoXXX throw exception; } } } |
ÒòΪstruts2ÌṩÁËÉùÃ÷ʽУÑéµÄ¹¦ÄÜ£¬¼´Ê¹ÓÃXMLÎļþ¶ÔÌá½»¹ýÀ´µÄÊý¾Ý½øÐÐУÑ飬¶øÕâÖÖÉùÃ÷ʽУÑé¾ÍÊÇÓÉactionValidatorManager.validate(action,context);Õâ¾ä´úÂëʵÏֵĵ÷ÓÃActionValidatorManagerµÄvalidate·½·¨£¬Æä·½·¨ÄÚ²¿¾ÍÊÇÈ¥²éÕÒÏàÓ¦µÄXMLУÑéÎļþ£¬½âÎöXMLУÑéÎļþÉú³Écom.opensymphony.xwork2.validator.Validator¼ìÑéÆ÷¶ÔÏó£¬È»ºó¶ÔÏóÌá½»µÄÊý¾Ý½øÐÐУÑ飬Èç¹ûУÑéÓÐЩÊý¾Ý²»ºÏ·¨Ôò»á½«ÏàÓ¦µÄ´íÎóÐÅϢͨ¹ýaddFieldErrorÌí¼Ó×ֶδíÎóÐÅÏ¢´òÓ¡µ½¿ØÖÆ̨¡£
ÒòΪÉùÃ÷ʽУÑ鹦ÄÜÉæ¼°Ñ°ÕÒXMLУÑéÎļþ£¬½âÎöXMLУÑéÎļþÉú³ÉУÑéÆ÷¶ÔÏó£¬ÔÙʹÓÃУÑéÆ÷¶ÔÏó½øÐÐУÑéÊý¾Ý£¬ÀïÃ滹Ìí¼ÓÁËУÑéÆ÷»º´æ£¬ËùÒÔÀïÃæµÄ´úÂëÁ¿ºÜ´ó£¬ÔÚÕâÖ»Äܽ²´ó¸ÅÔÀí£¬¾ßÌåϸ½ÚÓÐÐËȤ¿ÉÒÔ×Ô¼ºÈ¥Ñо¿¡£
Èç¹ûAction¼Ì³Ð×ÔActionSupportÀà(ͨ³£)£¬ÔòʵÏÖÁËValidateable½Ó¿Ú£¬½ÓÏÂÀ´¾Í»áµ÷ÓôøÓÐvalidate»òvalidateDoǰ׺µÄУÑé·½·¨£¬¾ÍÊÇͨ¹ýPrefixMethodInvocationUtilÕâ¸ö¹¤¾ßÀàµ÷Óõģ¬Õâ¸ö¹¤¾ßÀàÇ°ÃæÎÒÃÇÒѾÓöµ½¹ýÁË£¬ÔÚ½²½âPrepareInterceptorÀ¹½ØÆ÷µÄʱºò£¬»áµ÷ÓôøÓÐprepare»òprepareDoǰ׺µÄ·½·¨¡£´øÓÐvalidate»òvalidateDoǰ׺µÄУÑé·½·¨Èç¹ûͬʱ´æÔڵĻ°Ö»»áÖ´ÐдøÓÐvalidateǰ׺µÄ·½·¨£¬ÕâÊÇPrefixMethodInvocationUtilÕâ¸ö¹¤¾ßÀàÄÚ²¿´úÂë¾ö¶¨µÄ¡£
ValidationInterceptorµÄalwaysInvokeva lidateÊôÐÔĬÈÏΪtrue£¬ËùÒÔActionµÄvalidate·½·¨×ÜÊÇ»áµ÷Óõģ¬¼´validateable.validate();Õâ¾ä´úÂë»áÖ´ÐС£ÔÚvalidate·½·¨ÖÐʹÓôúÂë½øÐÐУÑé¡£
µ½ÕâÀïdoBeforeInvocation·½·¨¾ÍÖ´ÐÐÍê³ÉÁË£¬Õû¸öAnnotationValidationInterceptorÂß¼Ò²¾ÍÖ´ÐÐÍê³É£¬×îºóµ÷ÓÃinvocation.invoke();µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷......