【问题标题】:What do you need for the CDI capability in a JSF Project?JSF 项目中的 CDI 功能需要什么?
【发布时间】:2012-10-05 12:05:51
【问题描述】:

我有一个针对 WebSphere Application Server 8 的 JSF 项目,我尝试使用 CDI 概念。我有一个 /WEB-INF/beans.xml 喜欢:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " title="http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">" class="link">http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> (http://java.sun.com/xml/ns/javaee/beans_1_0.xsd)
</beans>

这是我的托管 bean:

@ConversationScoped
@Named("systemParameter")
public class SystemParameter implements Serializable {

    @EJB
    private ConfigFacade parameterFacade;

这是来自 Xhtml 页面的 sn-p:

<h:commandButton action="#{systemParameter.doSave}"/>

如果我尝试点击按钮,我会得到以下异常。

javax.el.PropertyNotFoundException: /systemParameters.xhtml at line 99 and column 99 action="#{systemParameter.doSave}": Target Unreachable, identifier 'systemParameter' resolved to null

我还需要什么?

【问题讨论】:

  • 您是否仔细检查了托管 bean 中的导入以确保您正在导入 CDI 注释?
  • @vcetinick 是的:-) import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.enterprise.context.ConversationScoped; import javax.inject.Named;
  • Java 区分大小写。 Beans.xmlbeans.xml 不同。文件位置也很重要,但你什么也没说。内容实际上无关紧要(可以保持为空),但是您显示了一些奇怪的节点值。
  • @BalusC 文件名是beans.xml。该位置在 WEB-INF 下。
  • 从 2003 年到 2008 年,我一直在使用 WebSphere 5.x/6.x。很高兴我不再使用那个奇怪的野兽了。

标签: java jsf-2 websphere java-ee-6 cdi


【解决方案1】:

在 WAS 8.x 上,确保使用 Apache MyFaces 2.x 实现来实现 EJB(以及与此相关的 CDI)的依赖注入。

【讨论】:

    【解决方案2】:

    我相信你应该用过

    @Inject
    private ConfigFacade parameterFacade
    

    而不是 @EJB 注释将会话 bean 注入 Web 控制器

    【讨论】:

      【解决方案3】:

      实际上,我至少可以分享一个可以帮助您找出问题所在的 sn-p。

      使用下面的

      @PostConstruct
      private void test() {
          Bean<?> bean = (Bean) manager.resolve(manager.getBeans("systemParameter"));
              System.out.println(bean);
      
      }
      

      现在如果经理打印 null 你可能有问题。尝试调试,

      在 println 处设置断点并检查管理器获取了多少个 bean。在 Eclipse 悬停管理器中单击 beans,检查底层数组,它有多大?回来报告。

      【讨论】:

        猜你喜欢
        • 2012-04-30
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-08
        • 2014-08-29
        相关资源
        最近更新 更多