Question

I've a pretty basic setup here:

@Named
@ApplicationScoped
public class TalentIdConverter implements Converter {
    @EJB
    private EntityManagerDao em;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (Strings.isNullOrEmpty(value)) {
            return null;
        }
        return em.find(Talent.class, Long.parseLong(value));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(((Talent) value).getId());
    }
}

// Manager.class
public class Manager {
    @Inject @Param(converterClass = TalentIdConverter.class, name = "talentId")
    private ParamValue<Talent> curTalent

    @PostConstruct
    public void init() {
        // use curTalent.getValue()
    }
}

But every time TalentIdConverter.getAsObject is called em is null. Can someone enlighten me why this is? I've also tried using @FacesConverter on the converter, but the behavior did not change.

This is on Wildfly-8.0.0.Beta1 using Weld-2.1.0.CR1 and Omnifaces-1.6.3

Was it helpful?

Solution

In the current 1.6.3 version, the @Param(converterClass) creates an unmanaged instance of the given converter class. It's like as if you're doing new TalentIdConverter() without any injection. If you need a managed instance, then you should in this specific case with a CDI-managed converter class (registered via @Named) be using @Param(converter) instead:

@Inject @Param(converter = "#{talentIdConverter}", name = "talentId")
private ParamValue<Talent> curTalent;

Or, if it's registered as a @FacesConverter("talentIdConverter") and thus a JSF-managed converter class (which also just transparently supports EJB when you've OmniFaces 1.6 installed):

@Inject @Param(converter = "talentIdConverter", name = "talentId")
private ParamValue<Talent> curTalent;

Or, if it's registered as a @FacesConverter(forClass=Talent.class), then you don't need to explicitly specify the converter anymore.

@Inject @Param(name = "talentId")
private ParamValue<Talent> curTalent;

On the other hand, trying to create a managed instance via BeanManager for @Param(converterClass) wouldn't have been a bad idea after all. We may look into this for future OmniFaces versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top