在 Spring+Tomcat 上使用 JSF 2.2.9 会导致 java.lang.NoClassDefFoundError: javax/enterprise/context/spi/Conte

Posted

技术标签:

【中文标题】在 Spring+Tomcat 上使用 JSF 2.2.9 会导致 java.lang.NoClassDefFoundError: javax/enterprise/context/spi/Contextual【英文标题】:Using JSF 2.2.9 on Spring+Tomcat causes java.lang.NoClassDefFoundError: javax/enterprise/context/spi/Contextual 【发布时间】:2015-03-31 17:02:19 【问题描述】:

我需要在 JSF 组件中使用自定义 html 属性,为此我找到的唯一解决方案是使用 JSF 2.2,以这种方式:

<f:passThroughAttribute name="data-toggle" value="modal" />

问题是我使用的是 Spring + Tomcat,我无法更改它,因为我没有时间。每次启动服务器时,都会出现以下错误:

Informações: CDI @ViewScoped manager unavailable
java.lang.NoClassDefFoundError: javax/enterprise/context/spi/Contextual

我知道会发生这种情况,因为我不使用 CDI,但正如我所说,由于时间原因,我无法从 Spring 更改为 CDI。

我该如何解决这个问题?我必须使用 JSF 2.2(因为自定义 html 属性)和 Spring。

已解决:

这个问题解决了,我降级到 JSF 2.2.6 并且错误停止了。

堆栈跟踪:

Fev 01, 2015 4:13:57 PM com.sun.faces.application.view.ViewScopeManager <init>
Informações: CDI @ViewScoped manager unavailable
java.lang.NoClassDefFoundError: javax/enterprise/context/spi/Contextual
at com.sun.faces.application.view.ViewScopeManager.<init>(Unknown Source)
at com.sun.faces.application.view.ViewScopeManager.getInstance(Unknown Source)
at com.sun.faces.application.view.ViewScopeEventListener.processEvent(Unknown Source)
at javax.faces.event.SystemEvent.processListener(Unknown Source)
at javax.faces.event.ComponentSystemEvent.processListener(Unknown Source)
at com.sun.faces.application.ApplicationImpl.processListeners(Unknown Source)
at com.sun.faces.application.ApplicationImpl.invokeListenersFor(Unknown Source)
at com.sun.faces.application.ApplicationImpl.publishEvent(Unknown Source)
at javax.faces.component.UIViewRoot.getViewMap(Unknown Source)
at javax.faces.component.UIViewRoot.getViewMap(Unknown Source)
at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.handle(Unknown Source)
at com.sun.faces.mgbean.BeanManager$ScopeManager.pushToScope(Unknown Source)
at com.sun.faces.mgbean.BeanManager.createAndPush(Unknown Source)
at com.sun.faces.mgbean.BeanManager.create(Unknown Source)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(Unknown Source)
at com.sun.faces.el.ManagedBeanELResolver.getValue(Unknown Source)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(Unknown Source)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(Unknown Source)
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at com.sun.faces.facelets.el.TagValueExpression.getValue(Unknown Source)
at javax.faces.component.UIComponentBase$AttributesMap.get(Unknown Source)
at com.sun.faces.el.CompositeComponentAttributesELResolver$ExpressionEvalMap.get(Unknown Source)
at javax.el.MapELResolver.getValue(MapELResolver.java:52)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(Unknown Source)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(Unknown Source)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at com.sun.faces.facelets.el.ContextualCompositeValueExpression.getValue(Unknown Source)
at com.sun.faces.facelets.el.TagValueExpression.getValue(Unknown Source)
at com.sun.faces.application.ApplicationImpl.createComponentApplyAnnotations(Unknown Source)
at com.sun.faces.application.ApplicationImpl.createComponent(Unknown Source)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.createComponent(Unknown Source)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(Unknown Source)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(Unknown Source)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(Unknown Source)
at com.sun.faces.facelets.tag.composite.ImplementationHandler.apply(Unknown Source)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(Unknown Source)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(Unknown Source)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFacelet.include(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFacelet.include(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(Unknown Source)
at com.sun.faces.facelets.tag.jsf.CompositeComponentTagHandler.applyCompositeComponent(Unknown Source)
at com.sun.faces.facelets.tag.jsf.CompositeComponentTagHandler.applyNextHandler(Unknown Source)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(Unknown Source)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(Unknown Source)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(Unknown Source)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(Unknown Source)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(Unknown Source)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(Unknown Source)
at com.sun.faces.facelets.tag.ui.DefineHandler.applyDefinition(Unknown Source)
at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFaceletContext$TemplateManager.apply(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFaceletContext.includeDefinition(Unknown Source)
at com.sun.faces.facelets.tag.ui.InsertHandler.apply(Unknown Source)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(Unknown Source)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(Unknown Source)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFacelet.include(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFacelet.include(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFacelet.include(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(Unknown Source)
at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(Unknown Source)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(Unknown Source)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(Unknown Source)
at com.sun.faces.facelets.impl.DefaultFacelet.apply(Unknown Source)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(Unknown Source)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
at com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
at javax.faces.webapp.FacesServlet.service(Unknown Source)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:72)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at br.com.jwebbuild.filter.LoginFilter.doFilter(LoginFilter.java:73)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: javax.enterprise.context.spi.Contextual
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
... 98 more

【问题讨论】:

请回答您自己的问题,而不是修改问题。稍后您可以接受自己的答案,每个人都清楚这个问题已经解决了。 @BalusC 我昨天在他发送给我时添加了堆栈跟踪(编辑同行评审),他正在使用 Mojarra 2.2.9 和 Tomcat 7,当他更改 bean 范围时问题也得到了解决到 sessionScoped (Mojarra 2.2.9) 但他不想更改范围,所以我想这个问题可能仍然存在 java.net/jira/browse/JAVASERVERFACES-3400 我让他回到 Mojarra 2.2.6。我回复你的评论只是为了通知你这个问题可能在 2.2.9 上没有解决 【参考方案1】:

我有同样的问题,并在谷歌上找到了你的问题(我使用 JSF 2.2.9),在 hwellmann 的回答下,我在我的 maven 项目的 pom.xml 上添加了这些依赖:

<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.2</version>
</dependency>

问题解决了,谢谢!

【讨论】:

【参考方案2】:

您是否在设置中包含cdi-api.jar

这并不意味着您必须在应用程序中使用 CDI,但 API 类必须存在以满足 JSF 的依赖关系。

【讨论】:

不是,我说我只是降级了jsf的版本。 @hwellmann 如果我们不使用 CDI,为什么还要添加它?!以及哪些 JSF 依赖项? @Tarik:那么您可以使用更新版本的 JSF 库进行各种修复吗?较新版本的 Mojarra 似乎会扫描所有类,因此需要所有“可选”依赖项(即使它们不会在运行时使用)。 PrimeFaces(Extensions) 有类似的“问题”:forum.primefaces.org/viewtopic.php?f=14&t=40960 和另一个我现在找不到的问题。 @Kukeltje 所以添加 CDI 仅适用于您所说的可选依赖项,而不是像 hwellmann 所说的“必须” 必须包含“api”(cdi-api.jar,就像@hwellmann 所说),以满足 mojarra 在更新版本中似乎正在执行的运行时类扫描。如果您不使用运行时,则不需要添加实现类。对于 PrimeFaces,例如条形码、qr、poi 没有单独的 api 罐子,所以很遗憾,即使您不使用它们,您也需要包含完整的罐子。如果这种 mojara 行为保持不变,像 PF 这样的组件集可能会分成“核心”组件和可选组件。但这种扫描甚至可能在 mojarra 中是可配置的。【参考方案3】:

您可能正在使用来自javax.faces.bean@ViewScoped,它是在JSF 2.0 中添加的。尝试使用来自 javax.faces.view 的注解,在 JSF 2.2 中添加。

【讨论】:

【参考方案4】:

使用 JSF 2.2.8 或将 tomcat 更改为 Apache TomEE,或配置 CDI+JSF-Tomcat。 使用 jsf 旧版本的最佳解决方案,因为在新版本中将依赖于 CDI

【讨论】:

以上是关于在 Spring+Tomcat 上使用 JSF 2.2.9 会导致 java.lang.NoClassDefFoundError: javax/enterprise/context/spi/Conte的主要内容,如果未能解决你的问题,请参考以下文章

Tomcat 上的 Spring Security/JSF/Hibernate 意外会话劫持?

在同一个 Tomcat 服务器上部署 JSF 1.2 和 JSF 2.0 应用程序

使用 tomcat 的 JSF2 日志

如何在 JSF 1.2 上使用 JSFUnit 获取 Spring IoC Bean

从Tomcat 7迁移到8时的jsf实现版本NullPointerException

(RC 4.3.4)中的rich:fileUpload 组件在Apache Tomcat7 和JSF 2.2 上工作吗? :“无法读取请求序言”