如何从Java 8使Wicket 7与java.time一起使用? [英] How can I bring Wicket 7 to work with java.time from Java 8?

查看:76
本文介绍了如何从Java 8使Wicket 7与java.time一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多bean,并且都使用LocalDate和LocalDateTime。 Wicket中的DateTextField和所有其他小部件(例如DatePicker)仅适用于java.util.Date。有什么方法可以将转换器注入到Wicket 7中,以便它使用LocalDate或LocalDateTime吗?

I have lots of beans and all use LocalDate and LocalDateTime. The DateTextField in Wicket and all other widgets (like the DatePicker) only work on java.util.Date. Is there any way to inject a converter into Wicket 7 so that it uses LocalDate or LocalDateTime?

bean看起来像这样:

The beans look like this:

public class SomeBean {
  Long id = null;
  LocalDate since = null;
  // plus getters and setters
}

Wicket表单当前使用CompoundPropertyModel

A Wicket form currently uses a CompoundPropertyModel

CompoundPropertyModel<SomeBean> model = new CompundPropertyModel<>( bean );


推荐答案

您可以包装 LocalDate 等模型在 IModel< java.util.Date> 中,例如

You can wrap your LocalDate and etc. models in a IModel<java.util.Date>, e.g.

public static class LocalDateModel implements IModel<java.util.Date> {
    private IModel<LocalDate> localDateModel;
    public LocalDateModel(IModel<LocalDate> localDateModel){
        this.localDateModel = localDateModel;
    }


    @Override
    public Date getObject() {
        return convertLocalDateToUtilDateSomehow(localDateModel.getObject());
    }

    @Override
    public void setObject(Date object) {
        localDateModel.setObject(convertUtilDateToLocalDateSomehow(object));
    }

    @Override
    public void detach() {
        localDateModel.detach();
    }
}

如果您将这样的模型输入表单组件您想使用它应该可以正常工作。

If you then feed models like this into the form components you want to use it should work just fine.

如果您想让 CompoundPropertyModel 自动提供这种包装模型,则需要扩展它并覆盖它的 CompoundPropertyModel#wrapOnInheritance(Component component)方法来推断需要包装模型。

If you want your CompoundPropertyModel to automatically provide such wrapping models, you need to extend it and override it's CompoundPropertyModel#wrapOnInheritance(Component component) method to infer that a wrapping model is needed. Something like

@Override
public <C> IWrapModel<C> wrapOnInheritance(Component component)
{
    IWrapModel<C> actualModel = super.wrapOnInheritance(component);
    if (actualModel.getObject() instanceOf LocalDate) {
        return new LocalDateModelButAlsoWrapping(actualModel);
    } else {
        return actualModel;
    }
}

其中 LocalDateModelButAlsoWrapping 只是上面示例 LocalDateModel 的扩展,但它也实现了 IWrapModel< T>

Where LocalDateModelButAlsoWrapping is unsurprisingly just an extension of LocalDateModel example above but which also implements IWrapModel<T>.

如果使用此扩展名而不是常规的 CompoundPropertyModel ,它将检测字段何时为 LocalDate 并提供组件模型(例如 DateTextField ),这些模型的包装形式类似于 java.util.Date 模型。

If you use this extension instead of your regular CompoundPropertyModel it would detect when fields are LocalDate and provide models to components (like your DateTextField) that are wrapped to look like java.util.Date models.

我给您的代码片段相当脏(您可能不应该让模型对象推断其类型),因为我只有提供它来说明一般机制,因此建议您设计自己的方式来推断预期的对象类型(例如,您可以检查 Component 参数是否为 DateTextField ),但这是我能想到的解决方案的一般方向。

The code snippet I gave you is rather dirty though (you should probably not get the model object to infer its type) as I have only provided it to illustrate the general mechanism, so I suggest you devise your own way to infer the type of object expected (e.g. you can check if the Component argument is a DateTextField), but this is the general direction of the solution that I can imagine.

这篇关于如何从Java 8使Wicket 7与java.time一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆