如何在Spring 3 MVC中允许空日期 [英] How to allow null date in Spring 3 MVC
本文介绍了如何在Spring 3 MVC中允许空日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经为日期绑定定义了一个全局格式化程序:
I've defined a global formatter for date bindings:
<annotation-driven conversion-service="conversionService" />
<beans:bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<beans:property name="converters">
<beans:list>
...
</beans:list>
</beans:property>
<beans:property name="formatters">
<beans:set>
<beans:bean id="dateFormatter"
class="org.springframework.format.datetime.DateFormatter">
<beans:constructor-arg><beans:value>dd/MM/yyyy</beans:value></beans:constructor-arg>
</beans:bean>
</beans:set>
</beans:property>
</beans:bean>
这是正常的,但现在我还需要一个特定的日期字段可以留空。如何配置格式化程序(或这个字段)来接受空值?
This is working fine but now I also need that a particular date field can be left blank. How can I configure the formatter (or this very field) to accept null value?
谢谢! :
推荐答案
您可以通过在 false 中设置 lenient for DateFormatter
You can do this by set lenient in false for DateFormatter
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="org.springframework.format.datetime.DateFormatter">
<constructor-arg>
<value>dd/MM/yyyy</value>
</constructor-arg>
<property name="lenient" value="false"/>
</bean>
</set>
</property>
</bean>
这篇关于如何在Spring 3 MVC中允许空日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文