是否有人遇到此问题:
我需要配置spring来识别基于区域设置的日期和数字格式,即以下用户行为应该有效:
EN
,然后数字格式1.23
应该有效,spring mvc将接受此格式并且没有触发有效错误。用户还可以使用日期格式MM/dd/yyyy
更改日期,并且不会引发任何有效错误,用户可以发布此表单。DE
,然后数字格式1,23
应该有效,spring mvc将接受此格式并且没有触发有效错误。用户还可以使用日期格式dd.MM.yyyy
更改日期,并且不会触发有效错误。用户可以发布此表单。我试图使用@DateTimeFormat(pattern="#{messageSource['date_format']}")
,(我在messages_(locale).properties中定义了date_format)但是看起来spring还不支持这个,请参阅JIRA ISSUE
是否有人遇到类似的问题并得到解决方案。
编写我自己的转换器并在org.springframework.format.support.FormattingConversionServiceFactoryBean
中注册它有帮助吗?我需要某种基于请求的转换器
答案 0 :(得分:4)
由于没有人回答我的问题,我只是发布了一个解决方案来解决这个问题,它可以帮助其他人:
RequestContextUtils.getLocale(request);
请求可以自动连接到请求范围类(注意,它仅适用于字段注入,而不适用于构造或设置器)。在这个课程中,我得到基于语言环境的日期/数字格式。在我的控制器中(我们实际上是一个abstractController)。我有这样的代码:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new LocalizedDateEditor(formatHelper));
}
formatHelper
是请求范围的bean。 LocalizedDateEditor
看起来像这样:
public class LocalizedDateEditor extends CustomDateEditor {
public LocalizedDateEditor(FormatHelper formatHelper) {
super(new SimpleDateFormat(formatHelper.getDefaultDateFormat()), true);
}
}
它告诉spring使用我的dateFormat。
就是这样。