jsf - 升级到 JSF 2.3 后,@Inject 在 @FacesConverter 中不起作用

标签 jsf primefaces jsf-2 ejb jsf-2.3

我将 Primefaces 6 应用程序迁移到 JBoss EAP 7.4,并升级到 Primefaces 10,并将 JSF2.2 升级到 JSF2.3。迁移之后,FacesConverter 中的 @Inject 没有被初始化,因此返回 null。这个问题可能是重复的( https://stackoverflow.com/a/47447140/1918516 ),建议的答案没有解决我的问题。

下面是我当前的项目结构。

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>

MyConverter.java

@FacesConverter(value = "MyConverter", managed = true)
@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class MyConverter implements Converter {
...
   @Inject
   private MyBean myBean;
}

MyBean.java

@Stateless
@Named
public class MyBean{
...
}

Jsf23Activator.java

@ApplicationScoped
@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class Jsf23Activator {

}

web.xml

<web-app 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-app_4_0.xsd"
             version="4.0">
    ...

    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN</param-name>
        <param-value>true</param-value>
    </context-param>
</web-app>

我还没有创建beans.xml,因为据我所知,新版本不需要它。另外,在创建一个时,我遇到了常见错误:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type MyDao with qualifiers @Default

  1. 我通过注入(inject) CDI.current().select(MyBean.class).get(); 使其适用于转换器; 但对我来说这只是一个解决方法

  2. 另一种方法是从 MyConverter 中创建一个 @Named 并在我的 jsf 中使用以下内容,但这对于 JSF 2.2 而不是 2.3 来说是理想的选择:

最佳答案

实现中缺少的部分是 beans.xml 文件,我故意不使用它,因为从版本 1.1 开始,它不是强制性的,正如我的问题中提到的。

看来,为了注入(inject) FacesConverter 内部,还需要创建以下 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>

关于jsf - 升级到 JSF 2.3 后,@Inject 在 @FacesConverter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68980829/

相关文章:

jsp - 创建扩展名为 .jsp 或 .xhtml 或 .jsf 的 JSF 页面有什么区别

javascript - jsf.ajax.request 是否忽略了 onevent 选项?

ajax - JSF ajax 命令按钮不更新 primefaces 选择列表

jsf - Primefaces 多个对话框与 closeOnEscape

jsf - 更新 ="@(.myClass)"中的 PrimeFaces 选择器如何工作?

jsf - 如何延迟加载与 p :Tab, 中包含的页面关联的 ManagedBean 仅在打开 Tab 时

jsf - 如何在具有全页布局的 Primefaces Extension 中实现菜单溢出?

java - 无法触发p :calendar's selectListener

jsf-2 - 验证错误后选择一个菜单的默认值

jsf-2 - p :commandButton p:confirm actionListener not triggering