当前位置: 代码迷 >> java >> java.lang.IllegalStateException:在此环境中CDI API不可用。 在org.omnifaces.config.BeanManager处
  详细解决方案

java.lang.IllegalStateException:在此环境中CDI API不可用。 在org.omnifaces.config.BeanManager处

热度:66   发布时间:2023-08-02 10:48:34.0

我正在尝试从托管Bean方法显示图像,该方法使用omnifaces通过<o:graphicImage>返回byte[] ,我在WEB_INF/lib部署了omnifaces 2.1,这是我的代码

注册.java

public byte[] getImage() throws IOException{
return Utils.toByteArray(Faces.getResourceAsStream("/resources/images/loader.gif"));


        }

Home.xhtml

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:o="http://omnifaces.org/ui">
<h:head>
<h:outputStylesheet library="css" name="style.css"></h:outputStylesheet>
    <style>
.center {
    align: center;
}
</style>

</h:head>
<h:body>
    <h:form>
    <table style="float:right;margin-top:5px;">
        <tr><td><h:commandLink action="/user/second.xhtml" style="float:right;">Second page</h:commandLink></td>
        <td><h:commandLink action="#{login.logout}" style="float:right;">Logout</h:commandLink></td></tr>
</table>        
    </h:form>
    <h:form>
        <div style="margin: auto;">
            <h:messages globalOnly="true" infoStyle="display:none" styleClass="error" style="list-style-type:none;"/>
            <h:messages globalOnly="true"  errorStyle="display:none" styleClass="green" style="list-style-type:none;"/>
            <h4 align="center">Welcome to JSF #{login.username} Dude</h4>

            <h3 align="center">User Profile</h3>
            <h:dataTable value="#{register.getUser(login.username)}" var="info"
                styleClass="center" style="margin:auto;" border="1">
                <h:column>
                <f:facet name="header">Image</f:facet>
                 <o:graphicImage value="#{register.getImage()}" type="jpg" dataURI="true"/>
                </h:column>

                <h:column>
                    <!-- column header -->
                    <f:facet name="header">Email</f:facet>
                    <!-- row record -->
                                        <h:outputText value="#{info.email}"
                                            rendered="#{not  register.edit}" />
<!--                    <h:inputText value="#{info.email}" rendered="#{not register.edit}" /> -->
                </h:column>
                <h:column>
                    <!-- column header -->
                    <f:facet name="header">First Name</f:facet>
                    <!-- row record -->

                                        <h:outputText value="#{info.first}" rendered="#{not  register.edit}" />
<!--                    <h:inputText value="#{info.first}" rendered="#{not register.edit}" /> -->
                </h:column>
                <h:column>
                    <!-- column header -->
                    <f:facet name="header">Last name</f:facet>
                    <!-- row record -->

                                        <h:outputText value="#{info.last}" rendered="#{not  register.edit}" />
<!--                    <h:inputText value="#{info.last}" rendered="#{not register.edit}" /> -->
                </h:column>
                <h:column>
                    <!-- column header -->
                    <f:facet name="header">Phone</f:facet>
                    <!-- row record -->

                                        <h:outputText value="#{info.phone}"
                                            rendered="#{not  register.edit}" />
<!--                    <h:inputText value="#{info.phone}" rendered="#{not register.edit}" /> -->
                </h:column>
                <h:column>
                    <h:commandButton value="Update" action="/user/Updateprofile.xhtml">

                    </h:commandButton>

                </h:column>

            </h:dataTable>
        </div>
    </h:form>


</h:body>
</html>

最初,tomcat开始显示此错误<o:graphicImage> Tag Library supports namespace: http://omnifaces.org/ui, but no tag was defined for name: graphicImage]当我将omnifaces.jar添加到构建路径中时<o:graphicImage> Tag Library supports namespace: http://omnifaces.org/ui, but no tag was defined for name: graphicImage]在日食中。 WEB-INF/libomnifaces.jar之后,它不是让tomcat启动并显示以下错误

   java.lang.ExceptionInInitializerError
    at org.omnifaces.application.OmniApplication.<init>(OmniApplication.java:69)
    at org.omnifaces.application.OmniApplicationFactory.createOmniApplication(OmniApplicationFactory.java:89)
    at org.omnifaces.application.OmniApplicationFactory.getApplication(OmniApplicationFactory.java:54)
    at org.apache.myfaces.config.FacesConfigurator.configureApplication(FacesConfigurator.java:1926)
    at org.apache.myfaces.config.FacesConfigurator.configure(FacesConfigurator.java:529)
    at org.apache.myfaces.webapp.AbstractFacesInitializer.buildConfiguration(AbstractFacesInitializer.java:296)
    at org.apache.myfaces.webapp.Jsp21FacesInitializer.initContainerIntegration(Jsp21FacesInitializer.java:73)
    at org.apache.myfaces.webapp.AbstractFacesInitializer.initFaces(AbstractFacesInitializer.java:118)
    at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: CDI API is not available in this environment.
    at org.omnifaces.config.BeanManager.<init>(BeanManager.java:88)
    at org.omnifaces.config.BeanManager.<clinit>(BeanManager.java:49)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: javax.enterprise.inject.spi.BeanManager
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.omnifaces.config.BeanManager.<init>(BeanManager.java:82)
    ... 19 more

Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.omnifaces.ApplicationListener
java.lang.NoClassDefFoundError: Could not initialize class org.omnifaces.config.BeanManager
    at org.omnifaces.ApplicationListener.checkCDIAvailable(ApplicationListener.java:63)
    at org.omnifaces.ApplicationListener.contextInitialized(ApplicationListener.java:55)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file
Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/First] startup failed due to previous errors
Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext listenerStop
SEVERE: Exception sending context destroyed event to listener instance of class org.apache.myfaces.webapp.StartupServletContextListener
java.lang.NoClassDefFoundError: Could not initialize class org.omnifaces.config.BeanManager
    at org.omnifaces.application.OmniApplication.<init>(OmniApplication.java:69)
    at org.omnifaces.application.OmniApplicationFactory.createOmniApplication(OmniApplicationFactory.java:89)
    at org.omnifaces.application.OmniApplicationFactory.getApplication(OmniApplicationFactory.java:54)
    at org.apache.myfaces.context.servlet.FacesContextImplBase.getApplication(FacesContextImplBase.java:131)
    at org.apache.myfaces.webapp.AbstractFacesInitializer._dispatchApplicationEvent(AbstractFacesInitializer.java:239)
    at org.apache.myfaces.webapp.AbstractFacesInitializer.destroyFaces(AbstractFacesInitializer.java:273)
    at org.apache.myfaces.webapp.StartupServletContextListener.contextDestroyed(StartupServletContextListener.java:153)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5063)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5723)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我正在使用JSF 2.0(Apache Myfaces) ,并且我还使用了战斧1.1.14来上传文件。

OmniFaces 2.x需要 。 另请参见 部分:

OmniFaces 2.x

必需 :Java 1.7,JSF 2.2,EL 2.2,Servlet 3.0和CDI 1.1
可选 :BV 1.0

Tomcat作为准系统JSP / Servlet容器并未随CDI一起提供。 仅在成熟的Java EE容器(如和 。 顺便说一句,这同样适用于JSF(和JSTL),您必须在Tomcat中手动安装它。

您有2个选择:

  1. 用TomEE或任何其他Java EE容器替换Tomcat。

  2. 在Tomcat中手动安装CDI。

也可以看看:

  • -包含详细的安装说明
  相关解决方案