问题
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