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

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

问题描述

AutoComplete演示中PlayersConverter的实现实际上不仅充当转换器,而且还充当Players列表的加载器.我对此模型有些厌倦,因为在我的项目中已经实现了加载. 我不明白为什么未将Converter接口实现为模板:

Converter<Players>

相反.

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

使用PrimeFaces转换器而不实际加载数据是否有可能?如何通知转换器应该转换的列表项类型?

解决方案

我了解您正在谈论PlayerConverter,如


从编辑之前的原始问题开始:

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

对于converter="player",它只是在faces-config.xml中注册的<converter-id>的值.您也可以通过@FacesConverter批注进行注册:

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

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


无关与具体问题无关,我知道整个转换器似乎有些不必要的代码重复. JSF实用工具OmniFaces库已经以 omnifaces.ListConverter 的形式识别并解决了此问题.只需使用converter="omnifaces.ListConverter"而不是converter="player"和整个转换器类即可.请注意,对于使用<f:selectItem(s)>的组件,有一个类似的转换器,即 omnifaces.SelectItemsConverter .

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>

instead.

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.

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?

解决方案

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:

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

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 {
    // ...
}

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?


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 Converter混淆(因为它适用于selectOneMenu)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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