与 PrimeFaces 转换器混淆(因为它适用于 selectOneMenu) [英] Confused with PrimeFaces Converter (as it applies to selectOneMenu)

查看:13
本文介绍了与 PrimeFaces 转换器混淆(因为它适用于 selectOneMenu)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AutoComplete 演示中 PlayersConverter 的实现实际上不仅用作转换器,还用作播放器列表的加载器.我对这个模型有点厌倦,因为我的项目中已经实现了加载.我不明白为什么 Converter 接口没有实现为模板:

The implementation of PlayersConverter in AutoComplete demo actually serves not only as a converter, but also as a loader for Players list. I am a bit weary of this model, as loading is already implemented in my project. I don't understand why Converter interface was not implemented as template:

Converter<Players>

相反.

是的,这些演示看起来很棒,但是看到数据被静态加载到转换器内部的列表中,我无法弄清楚如何在现实生活应用程序中使用它,转换器不应该与数据加载真正有任何关系.

Yes, these demos look great, but seeing data being statically loaded into the lists inside the converters I can't figure how to use that in real life application, where converter should not really have anything to do with data loading.

是否可以在不实际加载数据的情况下使用 PrimeFaces 转换器?我如何通知转换器它应该转换的列表项的类型?

Is it at all possible to use PrimeFaces converters without actually loading data in them? How can I inform a converter of the type of list item it is supposed to convert?

推荐答案

我知道您在谈论 PlayerConverter,如 此页面.这只是用于纯粹演示目的的极其本地化的实现(展示没有使用任何数据库,他们必须在某处获取这些数据).这确实令人困惑和误导.在现实世界的代码中,您应该与数据库中的数据进行交互,如下所示:

I understand that you're talking about PlayerConverter as shown in this page. This is just an extremely localized implementation for pure demonstration purposes (the showcase isn't using any DB and they have to get hold of those data somewhere). This is indeed confusing and misleading. In real world code, you should be interacting with the data from the database, something like follows:

@ManagedBean
public class PlayerConverter implements Converter {

    @EJB
    private PlayerService service;

    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        if (submittedValue == null || submittedValue.isEmpty()) {
            return null;
        }

        try {
            return service.find(Integer.valueOf(submittedValue));
        } catch (NumberFormatException exception) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player ID"));
        }
    }

    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
        if (modelValue == null) {
            return "";
        }

        if (modelValue instanceof Player) {
            return String.valueOf(((Player) modelValue).getNumber());
        } else {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player instance"));
        }
    }
}

<小时>

来自编辑前的原始问题:


From your original question before the edit:

此外,当他们在演示中写converter="player"时,player"指的是什么?

Further, when they write in their demos converter="player" what does "player" refer to?

对于converter="player",它只是faces-config.xml<中注册的的值/代码>.也可以通过@FacesConverter注解注册:

As to the converter="player", it's just the value of the <converter-id> as registered in faces-config.xml. You can also register it via @FacesConverter annotation:

@FacesConverter("player") // I'd rather rename it to playerConverter.
public class PlayerConverter implements Converter {
    // ...
}

请注意,我的示例使用 @ManagedBean 代替,否则将无法使用 @EJB.另请参阅 如何在@FacesConverter 中注入@EJB、@PersistenceContext、@Inject、@Autowired 等?

Note that my example uses @ManagedBean instead, otherwise using @EJB would not have been possible. See also How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?

与具体问题无关,我理解整个转换器似乎是某种不必要的代码重复.JSF 实用程序库 OmniFaces 在 omnifaces.ListConverter 的风格中识别并解决了这个问题.只需使用 converter="omnifaces.ListConverter" 而不是 converter="player" 和整个转换器类.请注意,使用 <f:selectItem(s)> 的组件有一个类似的转换器,omnifaces.SelectItemsConverter.

Unrelated to the concrete problem, I understand that the whole converter seems some kind of unnecessary code duplication. The JSF utility library OmniFaces has identified and solved this problem in flavor of omnifaces.ListConverter. Just use converter="omnifaces.ListConverter" instead of converter="player" and the whole converter class. Note that there's a similar converter for components using <f:selectItem(s)>, the omnifaces.SelectItemsConverter.

这篇关于与 PrimeFaces 转换器混淆(因为它适用于 selectOneMenu)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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