Injection in a converter does not work in JSF 2.3

为君一笑 提交于 2019-12-11 16:03:53

问题


Server: Payara 5.183.

When the converter is used, a NullPointerException is raised because the injected EJB is null (System.out.println prints "null").

It works (injection not null) if I use a workaround used before JSF 2.3: replacement of @FacesConverter by @Name.

Converter:

@FacesConverter(value = "compteConverter", managed = true)
public class CompteConverter implements Converter<CompteBancaire> {

  @EJB
  private GestionnaireCompte gestionnaireCompte;

  @Override
  public CompteBancaire getAsObject(FacesContext context, UIComponent component, String id) {
    if (id == null || id.isEmpty()) {
      return null;
    }
    try {
      System.out.println("*****EJB gestionnaireCompte=" + gestionnaireCompte);
      return gestionnaireCompte.getCompte(Long.parseLong(id));
    } catch (NumberFormatException e) {
      throw new ConverterException(new FacesMessage("Id de compte invalide"), e);
    }
  }

  @Override
  public String getAsString(FacesContext arg0, UIComponent arg1, CompteBancaire compte) { ... }

Usage of this converter:

  <ui:define name="metadata">
    <f:metadata>

      <f:viewParam name="id" value="#{operations.compte}"
                     converter="compteConverter"/>

Is it a bug of Mojarra/Payara (managed = true is not working) or can you help me to find my error?


回答1:


Managed converters don't work by default. To make them work I added a CDI bean annotated by @FacesConfig (for JSF 2.3 to be used) and @ApplicationScoped (it will be a CDI bean with this annotation).



来源:https://stackoverflow.com/questions/52511992/injection-in-a-converter-does-not-work-in-jsf-2-3

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