Inject a EJB into a JSF converter with JEE6 [duplicate]

做~自己de王妃 提交于 2019-11-27 22:17:05

I never used JSF 2.0 (only 1.0), but chapter 5.4 of the spec says:

[...] allow the container to inject references to container managed resources into a managed bean instance before it is made accessible to the JSF application. Only beans declared to be in request, session, or application scope are eligble for resource injection.

But so far I understand, a JNDI lookup should do the trick.

Other (yet not so pretty) solution may be using binding instead of converterId. Using JSF managed beans only:

<f:converter binding="#{app.personConverter}" />

Where appBean stands for something like: @ManagedBean(name="app") @ApplicationScoped class AppBean { @EJB private PersonService ps; private Converter personConverter; }

There MAY be a nicer solution in CDI-style (JSR-299) but i've failed to make this one running:

<f:converter binding="#{cdiBean}" />

Where cidBean ought to be: @Named class CdiBean implements Converter { @EJB ... }

Fails with 'Default behavior invoked of requiring a converter-id passed in the constructor'

Anyhow first approach using binding and app scoped JSF bean works.

The Seam Faces extension for JSF 2.0 and CDI allows @Inject support directly in Validators and Converters.

Check it out: http://ocpsoft.com/java/seam-faces-3-0-0-alpha2-jsf-2-0-just-got-even-easier/

i don't know if this solution is pretty... but it does work:

@ManagedBean
public class AcquisitionConverter implements Converter
{
    @EJB
    private AcquisitionService service;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        ...
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        ...
    }
}

and

<h:inputText value="#{flowController.acquisition}" converter="#{acquisitionConverter}">

with jsf 2.1.3 (mojarra) and glassfish 3.1.1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!