Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I inject in @FacesConverter? [duplicate]

I wrote a converter. I am using CDI and injection parallel. In that case the classes are not injected. How can I make the injection possible?

@FacesConverter(forClass = MyClass.class)

public class MyConverter implements Converter{

    @EJB
    private ClassForEJB classForEJB;

    @Inject
    private ClassForInject classForInject;

// Converter Methods
}
like image 440
Kayser Avatar asked Oct 31 '12 11:10

Kayser


1 Answers

The @FacesConverter isn't an eligible injection target. Replace it by @ManagedBean or @Named. As you'd like to use CDI as as well, use @Named.

@Named
@ApplicationScoped // I assume that your converter doesn't have any state.
public class MyConverter implements Converter {
    // ...
}

You only need to change the way it's been referenced in the views. You cannot rely on forClass anymore. You'd need to explicitly specify it as #{myConverter}.

<h:inputSomething ... converter="#{myConverter}" />

or

<h:inputSomething ...>
    <f:converter binding="#{myConverter}" />
</h:inputSomething>

If you really need to keep the @FacesConverter in favor of forClass, then you'd need to grab the EJB manually by JNDI. A concrete example is shown in this blog article. I can however not tell that for CDI beans.

The JSF guys have confirmed this embarrassing oversight and they will make the @FacesConverter an eligible injection target in upcoming JSF 2.2, see also JSF spec issue 763 JSF 2.3.

See also:

  • How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?

Update if you happen to use JSF utility library OmniFaces, or are open to using it, since its version 1.6, when just having OmniFaces JAR in /WEB-INF/lib, all @FacesConverters (and @FacesValidators) in your webapp automatically become eligible for CDI and EJB injection without any extra effort.

like image 132
BalusC Avatar answered Oct 22 '22 06:10

BalusC