12

Have the following code snippets:

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>

In result getting the exception:

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

Got it after changing faces-config.xml from ver 2.2 to ver 2.3 syntax.

Meaning, with faces-config.xml with the following content everything works fine:

<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 deployed on the Payara 4.1.2.172 (Full) server, and also added to pom.xml with "provided" scope.

....
<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>
....

I have checked all solutions that I was able to find during several hours, including different version of beans.xml:

  1. initially beans.xml was not present in the project - issue persist;
  2. added empty beans.xml - issue persist;
  3. added beans.xml with two different options of bean-discovery-mode - "all" and "annotated" - issue persist;

Content of \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>

Tested on local instances of Payara 4.1.2.172, GlassFish 5 (java ver 1.8.0_144), and remote instance of Payara 4.1.2.172 (java ver 1.8.0_131).

Thanks!

Note: Example projects like this one https://github.com/AnghelLeonard/JSF-2.3/tree/master/JSF23InjectInConverterValidator give the same error.

4

4 回答 4

18

我想发布一个完整的解决方案,为了使 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)每个项目添加一次,无需多次添加。

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

于 2017-11-23T03:34:53.183 回答
2

我遇到了这个问题,因为在更新 JSF 之后,我的类路径中仍然有这个 jar:

el-impl-2.1.2.jar

删除这个后,问题就消失了。

于 2019-10-11T05:22:50.663 回答
1

在 DirectoryBean 添加这一行:

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

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

于 2017-09-22T06:53:59.900 回答
0

解决方案2:

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

于 2018-11-07T13:57:40.780 回答