FacesContext 在带有 JSF 2.3 的 Wildfly 14 中不可注入(Mojarra,主模块)

Posted

技术标签:

【中文标题】FacesContext 在带有 JSF 2.3 的 Wildfly 14 中不可注入(Mojarra,主模块)【英文标题】:FacesContext not injectable in Wildfly 14 with JSF 2.3 (Mojarra, main module) 【发布时间】:2019-04-09 19:55:36 【问题描述】:

我有一颗豆子:

import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;

...

@Named
@ViewScoped
public class SimpleBean implements Serializable

    private static final long serialVersionUID = 1L;

    @Inject
    protected FacesContext facesContext;

    ...

根据

https://arjan-tijms.omnifaces.org/p/jsf-23.html#1316

这应该适用于 2.3 ...

部署到 Wildfly 14 时,会导致:

13:02:33,516 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 72) HHH000400: Using dialect: org.hibernate.dialect.mysql8Dialect
13:02:33,563 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 72) Envers integration enabled? : true
13:02:34,344 INFO  [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (ServerService Thread Pool -- 72) HHH000397: Using ASTQueryTranslatorFactory
13:02:34,531 WARN  [org.jboss.weld.Bootstrap] (MSC service thread 1-1) WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!
13:02:34,918 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC000001: Failed to start service jboss.deployment.unit."auth-portal.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."auth-portal.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1728)
    at org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1556)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type FacesContext with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject protected com.company.project.view.SimpleBean.facesContext
  at com.company.project.view.SimpleBean.facesContext(SimpleBean.java:0)

    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:378)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:290)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:143)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:164)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:526)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:64)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:62)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:62)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    at org.jboss.threads.JBossThread.run(JBossThread.java:485)

13:02:34,918 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "auth-portal.war")]) - failure description: "WFLYCTL0080: Failed services" => "jboss.deployment.unit.\"auth-portal.war\".WeldStartService" => "Failed to start service
    Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type FacesContext with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject protected com.company.project.view.SimpleBean.facesContext
  at com.company.project.view.SimpleBean.facesContext(SimpleBean.java:0)
"

我的 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">
    ...

问题

这里出了什么问题,你如何解决这个问题?

顺便说一句,这里的答案没有解决它:How to inject FacesContext with JSF 2.3 and TomEE?

【问题讨论】:

没有开箱即用,只有13:49:20,809 INFO [org.jboss.as.jsf] (ServerService Thread Pool -- 55) WFLYJSF0007: Activated the following JSF Implementations: [main]。但是,在其中一个 XHTML 上使用表达式 JSF Version = #facesContext.class.package.implementationTitle #facesContext.class.package.implementationVersion 时,将显示字符串 JSF Version = Mojarra 2.3.5.SP2 【参考方案1】:

根据

https://github.com/javaserverfaces/mojarra#activating-cdi-in-jsf-23

这就是答案:

默认情况下,对于 CDI 支持,JSF 2.3 将以 JSF 2.2 模式运行。甚至 当您使用与 JSF 2.3 兼容的 faces-config.xml 时。换一种说法, JSF 工件的注入和 EL 解析的新 JSF 2.3 特性 (spec issue 1316) 在您明确激活它之前不会起作用。 换句话说,@Inject FacesContext 默认情况下不起作用。这 为了让 JSF 2.3 完全向后兼容,这是必需的。

目前只有一种方法可以在 JSF 2.3 中激活 CDI,并且 特此使 JSF 2.3 以完整的 JSF 2.3 方式运行。放在 任意 CDI 托管 bean 上的 @FacesConfig 注释。为了 例如,一个通用的启动/配置 bean。

@FacesConfig
@ApplicationScoped
public class YourApplicationConfig 
    // ...

完整示例:

import javax.enterprise.context.ApplicationScoped;
import javax.faces.annotation.FacesConfig;

@FacesConfig
@ApplicationScoped
public class Jsf23Activator 
    // ...

您需要@ApplicationScoped 注释,否则它将不起作用。之后在我的启动中控制台最终显示:

14:23:28,805 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 82) Initializing Mojarra 2.3.5.SP2 for context '/blah'
14:23:30,415 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 82) Monitoring file:/C:/dev/servers/wildfly-14.0.1.Final/standalone/deployments/blah.war/WEB-INF/faces-config.xml for modifications

【讨论】:

这里的诀窍是缺少@ApplicationScoped...没有它,什么都不会发生。 这里提到:***.com/questions/45682309/… 是的。根据我的尝试,您不需要额外的version = FacesConfig.Version.JSF_2_3

以上是关于FacesContext 在带有 JSF 2.3 的 Wildfly 14 中不可注入(Mojarra,主模块)的主要内容,如果未能解决你的问题,请参考以下文章

JSF - JUnit FacesContext模拟测试

XSP JSF FacesContext addMessage 并不总是有效

如何在过滤器中检索 FacesContext

GlassFish 5 上带有 JSF 2.3 Web 应用程序的上下文根

JSF的生命周期

wildfly jsf 文件 上传后 可以下载 访问