【问题标题】:Injection in a converter does not work in JSF 2.3转换器中的注入在 JSF 2.3 中不起作用
【发布时间】:2019-03-01 21:26:07
【问题描述】:

服务器:Payara 5.183。

使用转换器时,会引发 NullPointerException,因为注入的 EJB 为 null(System.out.println 打印“null”)。

如果我使用 JSF 2.3 之前使用的解决方法,它可以工作(注入不为空):用 @Name 替换 @FacesConverter。

转换器:

@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) { ... }

这个转换器的用法:

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

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

这是 Mojarra/Payara 的错误(managed = true 不起作用)还是你能帮我找出我的错误?

【问题讨论】:

  • faces-config.xml中声明的版本正确的是2.3。
  • GIT-HUB 上的一个项目来重现问题:github.com/richard-grin/testConverter2。您可以在 AccountManager 类中更改数据库的定义。要重现该问题,请编辑 AccountConverter 以使用 @FacesConverter 而不是 Named 和 RequestScoped,并编辑 operations.xhtml 以使用 中的 converterId

标签: jsf cdi converters


【解决方案1】:

默认情况下托管转换器不起作用。为了使它们工作,我添加了一个由@FacesConfig(用于 JSF 2.3)和 @ApplicationScoped(它将是一个带有此注释的 CDI bean)注释的 CDI bean。

【讨论】:

  • 这行得通。我需要添加此处描述的步骤:stackoverflow.com/questions/45682309/…
  • 我会再次检查,但除了添加由@ApplicationScoped 注释的 CDI bean 之外,我不需要任何额外的步骤。
猜你喜欢
  • 2014-06-12
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2013-01-10
  • 2017-08-10
相关资源
最近更新 更多