如何更改 StringToDate 中的默认格式? Spring WebFlow

发布于 2024-09-11 02:31:08 字数 78 浏览 5 评论 0原文

Spring WebFlow 中的默认日期格式是“yyyy-MM-dd”。

如何更改为其他格式?例如“dd.mm.yyyy”。

Default date format in Spring WebFlow is "yyyy-MM-dd".

How to change to another format? "dd.mm.yyyy" for example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糖 2024-09-18 02:31:08

抱歉发晚了,但这就是你必须做的。 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
        }       
    }
}

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
        }       
    }
}
蒲公英的约定 2024-09-18 02:31:08

我通过创建这个 bean 实现了这一点:

@Component(value = "applicationConversionService") 公共类
ApplicationConversionServiceFactoryBean 扩展
FormattingConversionServiceFactoryBean {

<前><代码>@Override
protected void installFormatters(FormatterRegistry 注册表) {
// 注册Spring提供的默认日期格式化程序
registry.addFormatter(new DateFormatter("dd/MM/yyyy"));
} }

像这样注册 bean:

<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService">
    <constructor-arg ref="applicationConversionService" />
</bean>

然后引用者:

<mvc:annotation-driven conversion-service="applicationConversionService" />

并且:

<!-- 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" />

使用此配置,它对我来说工作得很好。我希望它有帮助。

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.

垂暮老矣 2024-09-18 02:31:08

我想一定是这样的:

StringToDate std = new StringToDate();
std.setPattern("dd.MM.yyyy");

I think it must be like this:

StringToDate std = new StringToDate();
std.setPattern("dd.MM.yyyy");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文