在JSF 2.3中无法在转换器中进行注入 [英] Injection in a converter does not work in JSF 2.3
问题描述
服务器:Payara 5.183。
Server: Payara 5.183.
使用转换器时,会引发NullPointerException,因为注入的EJB为null(System.out.println打印 null)
When the converter is used, a NullPointerException is raised because the injected EJB is null (System.out.println prints "null").
如果我使用在JSF 2.3之前使用的解决方法,则工作(注入不为null):用@Name替换@FacesConverter。
It works (injection not null) if I use a workaround used before JSF 2.3: replacement of @FacesConverter by @Name.
转换器:
@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"/>
这是Mojarra / Payara的bug( managed = true
无法正常工作)还是可以帮我找到我的错误?
Is it a bug of Mojarra/Payara (managed = true
is not working) or can you help me to find my error?
推荐答案
托管转换器无法正常工作默认。为了使它们起作用,我添加了一个@FacesConfig注释的CDI bean(用于JSF 2.3)和@ApplicationScoped(它将是带有此注释的CDI bean)。
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).
这篇关于在JSF 2.3中无法在转换器中进行注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!