klo*_*lor 6 java spring spring-mvc jodatime
我正在使用Spring 3.1和Joda-Time开发一个多语言应用程序.
我们假设我有一个这样的命令对象:
private class MyCommand {
private LocalDate date;
}
Run Code Online (Sandbox Code Playgroud)
当我使用英国或美国语言环境请求时,它可以正确解析并date使用相应的日期格式绑定而没有任何问题,例如分别为21/10/2013和10/21/13.但是,如果我有一些类似georgian的语言环境,new Locale("ka")它不会绑定有效日期21.10.2014.所以我需要挂钩Spring格式化程序才能在每个语言环境中提供自己的格式.我有一个bean可以从语言环境中解析日期格式.你能指点我正确的方向我该怎样才能做到这一点?
小智 2
你必须实现你自己的 org.springframework.format.Formatter
例子
public class DateFormatter implements Formatter<Date> {
public String print(Date property, Locale locale) {
//your code here for display
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, LocaleContextHolder.getLocale());
String out = df.format(date);
return out;
}
public Date parse(String source, Locale locale)
// your code here to parse the String
}
}
Run Code Online (Sandbox Code Playgroud)
在你的弹簧配置中:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
<property name="formatterRegistrars">
<set>
</set>
</property>
<property name="converters">
<set>
</set>
</property>
<property name="formatters">
<set>
<bean class="com.example.DateFormatter" />
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
Run Code Online (Sandbox Code Playgroud)
希望对您有帮助!
| 归档时间: |
|
| 查看次数: |
929 次 |
| 最近记录: |