jsf - 升级到 JSF 2.3 后,@Inject 在 @FacesConverter 中不起作用
问题描述
我将 Primefaces 6 应用程序迁移到 JBoss EAP 7.4 并升级到 Primefaces 10 和 JSF2.2 到 JSF2.3。在这次迁移之后,我们在 FacesConverter 中的 @Inject 没有被初始化,因此返回 null。这个问题可能是重复的(https://stackoverflow.com/a/47447140/1918516),建议的答案没有解决我的问题。
在我当前的项目结构下方。
面孔-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,因为据我所知,较新的版本不需要它。同样在创建一个时,我遇到了常见错误:
引起:org.jboss.weld.exceptions.DeploymentException:WELD-001408:带有限定符@Default 的 MyDao 类型的依赖关系不满足
我通过注入来为转换器工作,
CDI.current().select(MyBean.class).get();
但对我来说这只是一种解决方法另一种方法是从 MyConverter 中创建一个 @Named 并在我的 jsf 中使用以下内容,但这对于 JSF 2.2 而不是 2.3 来说是理想的:
<h:inputSomething ...> <f:converter binding="#{myConverter}" /> </h:inputSomething>
解决方案
实现的缺失部分是 beans.xml 文件,我从 1.1 版开始故意不使用它,这不是强制性的,正如我的问题中提到的那样。
看起来为了在 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>
推荐阅读
- wordpress - WordPress 发布来自所有来源的请求
- javascript - 无法读取我从 axios 获得的对象的属性
- javascript - 在 Reactjs 中的新行上显示每个数组项
- angular - Angular modal 弹出窗口出现在底部
- formula - 使用 Quicksight 的覆盖率和频率
- google-cloud-platform - 如何在 google 帐户上找到与费用相关的 gcp 服务名称?
- python - 如何在 python 中进行一般的电子邮件搜索
- php - 在 Laravel 6 中将 URL 参数发布到中间件
- c# - 检查实体是否包含记录
- python - 如何理解字节对编码?