问题
Default date format in Spring WebFlow is "yyyy-MM-dd".
How to change to another format? "dd.mm.yyyy" for example.
回答1:
Sorry for the late post but here is what you have to do. Spring Webflow does custom Data Binding. Its similar to how Spring MVC does it. The difference though is where it handles it. Spring MVC handles it on the controller level ( using the @InitBinder ).
Spring webflow does it on the binding level. Before executing a transition webflow will bind all parameter values to the object then validates the form (if validate="true") then invokes the transition on a successful validation.
What you need to do is to get webflow to change the means in which it binds a Date. You can do this by writing a custom converter.
First you will need a conversion service:
@Component("myConversionService")
public class MyConversionService extends DefaultConversionService {
public void MyConversionService() {
}
}
Webflow will use this service to determine what special binding needs to be accounted for. Now just write your specific date binder (keep in mind webflow defaults a date binder you will just override it).
@Component
public class MyDateToString extends StringToObject {
@Autowired
public MyDateToString(MyConversionService conversionService) {
super(Date.class);
conversionService.addConverter(this);
}
@Override
protected Object toObject(String string, Class targetClass) throws Exception {
try{
return new SimpleDateFormat("MM\dd\yyyy").parse(string);//whatever format you want
}catch(ParseException ex){
throw new ConversionExecutionException(string, String.class, targetClass, Date.class);//invokes the typeMissmatch
}
}
}
回答2:
I achieved that by creating this bean:
@Component(value = "applicationConversionService") public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {
@Override protected void installFormatters(FormatterRegistry registry) { // Register the default date formatter provided by Spring registry.addFormatter(new DateFormatter("dd/MM/yyyy")); } }
Registered the bean like this:
<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService">
<constructor-arg ref="applicationConversionService" />
</bean>
Then referenced by:
<mvc:annotation-driven conversion-service="applicationConversionService" />
And:
<!-- Enables custom conversion, validation and sets a custom vew factory creator on Spring Webflow -->
<webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService" validator="validator" view-factory-creator="mvcViewFactoryCreator" />
With this configuration it works fine for me. I hope it helps.
回答3:
I think it must be like this:
StringToDate std = new StringToDate();
std.setPattern("dd.MM.yyyy");
来源:https://stackoverflow.com/questions/3306997/how-to-change-default-format-in-stringtodate-spring-webflow