webwork日期验证&Invalid field value for field Birthday

1.webwork源码分析:

com.opensymphony.xwork 
Interface XWorkMessages

public static final String ACTION_EXECUTION_ERROR = "xwork.error.action.execution";
public static final String MISSING_ACTION_EXCEPTION = "xwork.exception.missing-action";
public static final String MISSING_PACKAGE_ACTION_EXCEPTION = "xwork.exception.missing-package-action";
public static final String DEFAULT_INVALID_FIELDVALUE = "xwork.default.invalid.fieldvalue";

com\opensymphony\xwork\xwork-messages.properties
xwork.error.action.execution=Error during Action invocation
xwork.exception.missing-action=There is no Action mapped for action name {0}. Check if there is such an action name defined in xwork.xml and also if the such an action class exists. Check also the log to see if the action class is successfully loaded.
xwork.exception.missing-package-action=There is no Action mapped for namespace {0} and action name {1}. Check if there is such an action name with such namespace defined in the xwork.xml and also if such an action class exists. Check also the log to see if the action class is successfully loaded.
xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".


com.opensymphony.xwork.util 
Class XWorkConverter (详见getConversionErrorMessage方法)
java.lang.Object
  ognl.DefaultTypeConverter
      com.opensymphony.xwork.util.XWorkConverter
All Implemented Interfaces: 
ognl.TypeConverter 

com.opensymphony.xwork.validator.validators 
Class ConversionErrorFieldValidator (详见validate方法)
java.lang.Object
  com.opensymphony.xwork.validator.validators.ValidatorSupport
      com.opensymphony.xwork.validator.validators.FieldValidatorSupport
          com.opensymphony.xwork.validator.validators.RepopulateConversionErrorFieldValidatorSupport
              com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator
All Implemented Interfaces: 
FieldValidator, ShortCircuitableValidator, Validator 

从validators.xml代码中:
com.opensymphony.xwork.validator.validators 
Class DateRangeFieldValidator
java.lang.Object
  com.opensymphony.xwork.validator.validators.ValidatorSupport
      com.opensymphony.xwork.validator.validators.FieldValidatorSupport
          com.opensymphony.xwork.validator.validators.AbstractRangeValidator
              com.opensymphony.xwork.validator.validators.DateRangeFieldValidator
All Implemented Interfaces: 
FieldValidator, ShortCircuitableValidator, Validator 

 
2.例子如下:
freemarker:

<@ww.form action="test" method="post" validate="true">
     <@ww.textfield label="Birthday" name="Birthday" />
     <@ww.submit />

 
action:

  private Date Birthday;
  ...
  public Date getBirthday() {
      return Birthday;
  }
  public void setBirthday(Date birthday) {
      Birthday = birthday;
  }

 
action名字-validation.xml:
 

   
        
                必须为日期类型
        
    

 
如果我们在Birthday文本框输入: 222,这是一种非法日期格式,应该验证提示信息为:必须为日期类型
但是:验证提示信息为:Invalid field value for field "Birthday".
理解为什么提示该信息,就需要理解上面列出来的webwork源码中关系.

 


解决方法:在本地工程国际化资源文件中增加:
xwork.default.invalid.fieldvalue = Must be date format
xwork.default.invalid.fieldvalue = \u5FC5\u987B\u4E3A\u65E5\u671F\u7C7B\u578B
这样就会提示我们设置的提示信息了.控制台却报错.


产生原因:date validator字段校验器,检查指定的日期是否在指定的范围内.而不是用来验证日期的格式是否正确,提示信息在webwork系统代码抛出.

 
  
  
      1900-01-01
      2008-01-01
      Birthday must be within ${min} and ${max}
  
 
 
 
     Birthday
     ^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$
     Birthday日期格式错误
 

 
并且控制台没有报错,问题已解决.

 

3.版本:
 webwork-2.2.4.jar
 xwork-1.2.1.jar
 spring-2.0-rc2.jar

 

4.日期格式验证必须为webwork客户端验证,如果为服务器端验证,提示信息仍为:Invalid field value for field "Birthday".
而不是通过正则表达式的验证提示信息.

你可能感兴趣的:(开源框架)