具有以下代码段:

bean 角,扁 bean :

import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named(value = "directoryBean")
@ViewScoped
public class DirectoryBean implements Serializable {

private static final long serialVersionUID = 1L;
    ....
}

faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    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/web-facesconfig_2_3.xsd"
    version="2.3">
     ....
</faces-config>

group.xhtml
<ui:composition ...>

    <f:metadata>
        <f:viewParam name="id" value="#{directoryBean.id}" />
    </f:metadata>

</ui:composition>

结果得到异常:
javax.el.PropertyNotFoundException: /group.xhtml @6,64 value="#{directoryBean.id}": Target Unreachable, identifier 'directoryBean' resolved to null

将faces-config.xml从2.2版语法更改为2.3版语法后,将其获取。

意思是,使用带有以下内容的faces-config.xml,一切正常:
<faces-config version="2.2" 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/web-facesconfig_2_2.xsd">
....
</faces-config>

JSF 2.3.2部署在Payara 4.1.2.172(完整)服务器上,并且还添加到pom.xml中,具有“提供的”范围。
....
<dependencies>
    ...
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.3.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.3</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    ...
</dependencies>
....

我已经检查了几个小时内能够找到的所有解决方案,包括不同版本的beans.xml:
  • 最初在项目中不存在beans.xml-问题
    坚持;
  • 添加了空的beans.xml-问题仍然存在;
  • 在bean.xml中添加了两个不同的bean-discovery-mode选项:“全部”
    和“带注释”-问题仍然存在;

  • \WEB-INF\beans.xml的内容:
    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    

    在Payara 4.1.2.172,GlassFish 5(Java版本1.8.0_144)和Payara 4.1.2.172(Java版本1.8.0_131)的远程实例上进行了测试。

    谢谢!

    注意:像这样的示例项目https://github.com/AnghelLeonard/JSF-2.3/tree/master/JSF23InjectInConverterValidator给出了相同的错误。

    最佳答案

    我想发布一个完整的解决方案,为了使JSF 2.3库能够在JSF v2.3模式下工作,应该怎么做。下面的代码示例基于GlassFish 5.0服务器环境。

    1)至少将JSF库升级到版本2.3.3(它修复了与jsf 2.3模式激活有关的一些错误)

    2)beans.xml应该看起来像:

    <?xml version="1.0" encoding="UTF-8"?>
    <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_2_0.xsd"
       bean-discovery-mode="all" version="2.0">
    </beans>
    

    3)faces-config.xml应该看起来像:
    <?xml version='1.0' encoding='UTF-8'?>
    <faces-config version="2.3"
              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/web-facesconfig_2_3.xsd">
        ....
    </faces-config>
    

    4)所有这些设置中的关键角色-是特殊形式的Java类,实际上激活了JSF 2.3模式,在我的情况下,它具有名称Jsf23Activator和绝对为空的内容:
    package ua.local.beans;
    
    import javax.enterprise.context.ApplicationScoped;
    import javax.faces.annotation.FacesConfig;
    
    @ApplicationScoped
    @FacesConfig(version = FacesConfig.Version.JSF_2_3)
    public class Jsf23Activator {
    
    }
    

    注释@FacesConfig(version = FacesConfig.Version.JSF_2_3)每个项目添加一次,无需多次添加。

    基本上,其他人多次提到了添加此批注的需要,但是在我的情况下,直到我通过添加批注@ApplicationScoped将该类声明为CDI bean时,它才起作用。只有在将类声明为CDI bean之后,才清除项目/重新启动服务器-终于激活了JSF 2.3模式,现在我可以注入(inject)JSF类/利用其他JSF 2.3功能了!

    10-08 08:35
    查看更多