Spring WebFlow 中的默认日期格式是“yyyy-MM-dd”。
如何更改为其他格式?例如“dd.mm.yyyy”。
抱歉发晚了,但这就是你必须做的。 Spring Webflow 进行自定义数据绑定。它与 Spring MVC 的做法类似。但不同之处在于它处理它的位置。 Spring MVC 在控制器级别处理它(使用 @InitBinder )。
Spring webflow 在绑定级别上执行此操作。在执行转换之前,Webflow 会将所有参数值绑定到对象,然后验证表单(如果 validate="true"),然后在成功验证时调用转换。
您需要做的是让 webflow 更改它绑定日期的方式。您可以通过编写自定义转换器来做到这一点。
首先,您需要转换服务:
@Component("myConversionService")
public class MyConversionService extends DefaultConversionService {
public void MyConversionService() {
}
}
Webflow 将使用此服务来确定需要考虑哪些特殊绑定。现在只需编写您的特定日期活页夹(请记住,webflow 默认有一个日期活页夹,您只需覆盖它即可)。
@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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)