将 faces-config.xml 从 2.2 更改为 2.3 会导致 javax.el.PropertyNotFoundException:目标无法访问,标识符“bean"解析为 null [英] Changing faces-config.xml from 2.2 to 2.3 causes javax.el.PropertyNotFoundException: Target Unreachable, identifier 'bean' resolved to null

查看:23
本文介绍了将 faces-config.xml 从 2.2 更改为 2.3 会导致 javax.el.PropertyNotFoundException:目标无法访问,标识符“bean"解析为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下代码片段:

豆子:

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

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

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 版语法后得到它.

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

意思是,使用带有以下内容的 faces-config.xml 一切正常:

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 部署在 Payara 4.1.2.172 (Full) 服务器上,并且还添加到 pom.xml 的提供"范围.

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

我已经检查了几个小时内能够找到的所有解决方案,包括不同版本的 beans.xml:

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

  1. 最初项目中不存在 beans.xml - 问题坚持;
  2. 添加空 beans.xml - 问题持续存在;
  3. 添加了 beans.xml 和 bean-discovery-mode 的两个不同选项 - "all"和注释" - 问题仍然存在;

WEB-INFeans.xml 的内容:

Content of WEB-INFeans.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)上进行了测试.

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

谢谢!

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

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

推荐答案

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

I would like to post a complete solution, what should be done in order to make JSF 2.3 libs work in JSF v2.3 mode. Code samples below are based on GlassFish 5.0 server environment.

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

1) Upgrade JSF libs to the version 2.3.3 at least (it fixes some bugs related to jsf 2.3 mode activation)

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

2) The beans.xml should look like:

<?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 应该看起来像:

3) faces-config.xml should look like:

<?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 并且内容完全为空:

4) And the key-player in all this setup - is specially formed Java class that actually activates JSF 2.3 mode, in my case it has name Jsf23Activator and absolutely empty content:

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)每个项目添加一次,不需要多次添加.

The annotation @FacesConfig(version = FacesConfig.Version.JSF_2_3) is added once per project, no need to add it several times.

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

Basically the need to add this annotation was mentioned several times by others, but in my case it didn't work until I declared this class as CDI bean by adding annotation @ApplicationScoped. Only after I declared the class as CDI bean, cleared project / restarted server - the JSF 2.3 mode finally got activated and now I am able to inject JSF classes / utilize other JSF 2.3 features!

这篇关于将 faces-config.xml 从 2.2 更改为 2.3 会导致 javax.el.PropertyNotFoundException:目标无法访问,标识符“bean"解析为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆