今天,参考张孝祥的validator 方面的视频,简单了对struts validator 入门了下。
在对日期验证时,遇到了个问题,在上网搜索了一些方案,也没从根本上解决。
内容是这样的.
validator.xml 内容如下:
..........
<field property= "birthday" depends= "date">
<arg key= "input.birthday" position= "0"/>
<var>
<var-name>datePatternStrict</var-name>
<var-value>yyyy-MM-dd</var-value>
</var>
</field>
</form>
.......
<field property= "birthday" depends= "date">
<arg key= "input.birthday" position= "0"/>
<var>
<var-name>datePatternStrict</var-name>
<var-value>yyyy-MM-dd</var-value>
</var>
</field>
</form>
.......
然后验证表单时候,required约束能够正常进行对表单的约束,但是对于date约束,一旦在表单提交的字符串不为MM/dd/yyyy类型时候,不是在jsp页面显示错误信息,而是在console抛出异常,并且页面什么都不显示。
抛出的异常如下:
18:47:49,654 WARN GenericTypeValidator:222 - a
java.text.ParseException: Unparseable date: "a"
at java.text.DateFormat.parse(DateFormat.java:335)
at org.apache.commons.validator.GenericTypeValidator.formatDate(GenericTypeValidator.java:213)
at org.apache.struts.validator.FieldChecks.validateDate(FieldChecks.java:546)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.commons.validator.ValidatorAction.executeValidationMethod(ValidatorAction.java:567)
at org.apache.commons.validator.Field.validateForRule(Field.java:827)
at org.apache.commons.validator.Field.validate(Field.java:906)
at org.apache.commons.validator.Form.validate(Form.java:174)
问:这是怎么回事??怎么解决?
抛出的异常如下:
18:47:49,654 WARN GenericTypeValidator:222 - a
java.text.ParseException: Unparseable date: "a"
at java.text.DateFormat.parse(DateFormat.java:335)
at org.apache.commons.validator.GenericTypeValidator.formatDate(GenericTypeValidator.java:213)
at org.apache.struts.validator.FieldChecks.validateDate(FieldChecks.java:546)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.commons.validator.ValidatorAction.executeValidationMethod(ValidatorAction.java:567)
at org.apache.commons.validator.Field.validateForRule(Field.java:827)
at org.apache.commons.validator.Field.validate(Field.java:906)
at org.apache.commons.validator.Form.validate(Form.java:174)
问:这是怎么回事??怎么解决?
解决步骤:
将struts validator框架提供的 datePattern 修改为datePatternStrict.也没有解决问题。
问题搁浅了,希望有兴趣的朋友、或者高手帮忙 看看。
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/243025,如需转载请自行联系原作者