将faces-config.xml从2.2更改为2.3会导致javax.el.PropertyNotFoundException:目标无法访问,标识符'bean'已解析为null

问题描述 投票:5回答:3

请准备以下代码段:

豆:

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从ver 2.2更改为ver 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:

  1. 最初beans.xml不存在于项目中 - 问题仍然存在;
  2. 添加空beans.xml - 问题持续存在;
  3. 添加了beans.xml,它有两个不同的bean-discovery-mode选项 - “all”和“annotated” - 问题持续存在;

\ 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 ver 1.8.0_144)的本地实例,以及Payara 4.1.2.172(java ver 1.8.0_131)的远程实例。

谢谢!

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

jsf cdi faces-config propertynotfoundexception jsf-2.3
3个回答
7
投票

我想发布一个完整的解决方案,为了使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模式终于被激活了,现在我能够注入JSF类/利用其他JSF 2.3功能!


0
投票

在DirectoryBean中添加以下行:

// Activates CDI build-in beans
 @FacesConfig(
         version = JSF_2_3
)

并在beans.xml中将bean-discovery-mode更改为“all”。 faces-config.xml设置版本2.3


0
投票

解决方案2:

切换到Payara 5.183,它开箱即用。无需解决方案1:Jsf23Activator

© www.soinside.com 2019 - 2024. All rights reserved.