【问题标题】:CDI Bean method not called when using myfaces bundled in webapp and run on Wildfly使用捆绑在 webapp 中并在 Wildfly 上运行的 myfaces 时未调用 CDI Bean 方法
【发布时间】:2020-09-01 00:35:46
【问题描述】:

我想将我的 JSF 应用程序从 ManagedBean 迁移到 CDI Bean。

首先,我做了一个简单的测试,看看 CDI 是否正常工作,但它们没有。 这是我的示例,使用 Wildfly 10 和 myfaces 2.2

.\WEB-INF 中的beans.xml

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

xhtml 页面:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>
        test
    </title>
</h:head>

<h:body>
    <h:outputText value="#{CDITest.hello}"/>
    <h:outputText value="test"/>
</h:body>

</html>

支持 Bean

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import java.io.Serializable;

  @Named("CDITest")
  @SessionScoped
  public class CDITest implements Serializable{

    public String getHello(){
      return "Hello";
    }
}

输出

测试

没有错误消息 (!) 并且没有调用 CDITest.getHello() 方法。我错过了什么?

【问题讨论】:

  • 在 JSF 开发模式下运行你的应用程序
  • 你的意思是 javax.faces.PROJECT_STAGE?这是发展
  • 和一个视图源显示?
  • 测试
  • 顺便说一句,wildfly 10 和 myfaces?不是默认组合...您可以尝试启用他们的默认 JSF 吗?

标签: jsf wildfly cdi myfaces


【解决方案1】:

这个问题更普遍。 在 JSF 2.3 中,JSF 通过 BeanManager#getELResolver 获取 CDI。在 JSF 2.3 之前,容器或 CDI impl 必须结合 JSF 和 CDI。

【讨论】:

    【解决方案2】:

    我认为您需要在 JSF 2.3 中声明一个带 @FacesConfig 注释的类来激活 CDI

    import javax.enterprise.context.ApplicationScoped;
    import javax.faces.annotation.FacesConfig;
    
    @FacesConfig
    @ApplicationScoped
    public class ConfigurationBean {
    
    }
    

    【讨论】:

    • 就像在 stackoverflow 的多个副本中提到的那样。 (在最近的版本中不再需要这个 btw)问题是 OP 正在使用 MyFaces 2.2。
    【解决方案3】:

    在 Wildfly 19.0.0 上升级到 myfaces-2.3.6 (jsf 2.3) 解决了这个问题。请注意,您需要 @Cristyan 建议的 @FacesConfig 类。

    另请注意,使用 Mojarra 并没有发生此问题,并且 Bean 按预期工作(对于 Widlfly 10 和 Widlfly 19)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2014-05-09
      • 2022-07-20
      • 2015-04-20
      相关资源
      最近更新 更多