StrutsConversionErrorInterceptorÀ¹½ØÆ÷¼ò½é
StrutsConversionErrorInterceptorÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚÊ®ÁùµÄλÖã¬ÊÇÓÃÓÚ´¦ÀíתÀàÐÍת»»´íÎóµÄ£¬¸ÃÀ¹½ØÆ÷¼Ì³Ð×ÔConversionErrorInterceptorÀàÆä´ó²¿·Ö¹¦ÄÜÂß¼¶¼ÔÚConversionErrorInterceptorÖУ¬StrutsConversionErrorInterceptorÖ»ÊǶÔÆäÖеÄÁ½¸ö·½·¨½øÐÐÁ˸²¸Ç£¬·Ö±ðÊÇgetOverrideExprÓëshouldAddError·½·¨£¬ÒòΪConversionErrorInterceptorÀàÊÇxwrokÌṩµÄ£¬¶østruts2ÈÏΪÕâÁ½¸ö·½·¨²»ÊÇÄÇôºÏÀíËùÒÔ½øÐÐÁ˸²¸Ç¡£ËùÒÔÕæÕýÖ´Ðе½StrutsConversionErrorInterceptorµ÷ÓõÄÊǸ¸ÀàConversionErrorInterceptorµÄintercept·½·¨£¬ÏÂÃæÊǸ÷½·¨Ô´Â룺
±í1
@Override public String intercept(ActionInvocation invocation) throws Exception { //»ñÈ¡ActionContext¶ÔÏó ActionContext invocationContext = invocation.getInvocationContext(); //»ñÈ¡ActionContext¶ÔÏóÖеÄconversionErrors Map£¬ËùÒÔÀàÐÍת»»´íÎóÐÅÏ¢¶¼·ÅÔÚ¸ÃMapÖÐ Map<String, Object> conversionErrors = invocationContext.getConversionErrors(); ValueStack stack = invocationContext.getValueStack();//»ñÈ¡ÖµÕ»
HashMap<Object, Object> fakie = null; //µü´úÀàÐÍת»»´íÎó for (Map.Entry<String, Object> entry : conversionErrors.entrySet()) { String propertyName = entry.getKey();//ת»»´íÎóµÄÇëÇó²ÎÊýÃû Object value = entry.getValue();//ת»»´íÎóµÄÇëÇó²ÎÊýÖµ
if (shouldAddError(propertyName, value)) {//ÅжÏÊÇ·ñÒªÌí¼Ó´íÎóÐÅÏ¢ //»ñÈ¡´íÎóÌáʾÐÅÏ¢ String message = XWorkConverter.getConversionErrorMessage(propertyName, stack); //»ñÈ¡µ±Ç°Action Object action = invocation.getAction(); if (action instanceof ValidationAware) {//Ö»ÓÐActionʵÏÖÁËValidationAware½Ó¿Ú ValidationAware va = (ValidationAware) action; //²Å»á½«´íÎóÐÅÏ¢Ìí¼Óµ½FieldError¼¯ºÏÖУ¬ÒòΪûÓÐʵÏÖValidationAware½Ó¿Ú¸ù±¾¾ÍûÓÐaddFieldError·½·¨ va.addFieldError(propertyName, message); }
if (fakie == null) { fakie = new HashMap<Object, Object>(); } //½«´íÎóÖµÌí¼Óµ½fakieMapÖÐ(fakieÒâ˼ÊÇ£º¼Ù»õ£¬Æ×Ó) fakie.put(propertyName, getOverrideExpr(invocation, value)); } } //Èç¹ûfakieÓÐÖµ if (fakie != null) { // ½«fakie Map·Åµ½ActionContextÖÐ stack.getContext().put(ORIGINAL_PROPERTY_OVERRIDE, fakie); //×¢²áÒ»¸öPreResultListener¼àÌýÆ÷ invocation.addPreResultListener(new PreResultListener() { publicvoid beforeResult(ActionInvocation invocation, String resultCode) { Map<Object, Object> fakie = (Map<Object, Object>) invocation.getInvocationContext().get(ORIGINAL_PROPERTY_OVERRIDE);
if (fakie != null) { invocation.getStack().setExprOverrides(fakie); } } }); } return invocation.invoke();//µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ } |
ÕâÀïÖ»ÓÐActionʵÏÖÁËValidationAware½Ó¿Ú²ÅÄܽ«ÀàÐÍת»»´íÎóÐÅÏ¢Ìí¼Óµ½FieldErrorÖУ¬Ìṩ¸ø×îºóÒ»¸öÀ¹ÔØÆ÷£ºDefaultWorkflowInterceptorʹÓ㬶øÇÒÓÐÀàÐÍת»»´íÎó»¹»á×¢²áÒ»¸öPreResultListener¼àÌýÆ÷£¬¸Ã¼àÌýÆ÷»áÔÚActionÓëÀ¹½ØÆ÷Ö´ÐÐÍê±ÏÖ®ºó£¬ResultÖ´ÐÐ֮ǰִÐУ¬Õâ¸ö¼àÌýÆ÷¾ÍÊÇ°Ñ´æ´¢ÔÚActionContextÖеÄfakie MapÈ¡³öÀ´µ÷ÓÃValueStackµÄsetExprOverrides·½·¨ÉèÖýøÐÐÖµÕ»ÖУ¬µ±ÄãÔÙÒ³ÃæÖÐͨ¹ýOGNL±í´ï»ñÈ¡¸ÃÀàÐÍת»»³ö´íµÄ²ÎÊýʱ·µ»ØµÄÊDzÎÊýÔÀ´µÄÖµ¡£
ÀýÈçÓÐÒ»ÇëÇó²ÎÊýpath?age=xxx£¬¶øÔÚActionÖÐÊÇÓÃintÀàÐÍÈ¥½ÓÊյģ¬ÕâÑù¾Í»á±¨×ª»»´íÎ󣬵±ÄãÔÚÒ³ÃæÖÐÓÃOGNL»ñÈ¡ageÖµ(<s:propertyvalue="age"/>)ʱ·µ»ØµÄÊÇxxx¶ø²»ÊÇ0¡£
µ½´Ë¸ÃÀ¹½ØÆ÷¾Í½²ÍêÁË£¬×îºóinvocation.invoke();µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷......