Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf javax.enterprise.event.event的空指针异常_Jsf_Maven_Events_Nullpointerexception_Cdi - Fatal编程技术网

Jsf javax.enterprise.event.event的空指针异常

Jsf javax.enterprise.event.event的空指针异常,jsf,maven,events,nullpointerexception,cdi,Jsf,Maven,Events,Nullpointerexception,Cdi,当我试图在JavaEE应用程序中触发事件时,我得到一个空指针异常。下面是代码 HelloEvent事件模型: package com.eventhandling; import java.io.Serializable; import java.util.logging.Logger; public class HelloEvent implements Serializable { private static final long serialVersionUID = 1L;

当我试图在JavaEE应用程序中触发事件时,我得到一个空指针异常。下面是代码

HelloEvent
事件模型:

package com.eventhandling;

import java.io.Serializable;
import java.util.logging.Logger;


public class HelloEvent implements Serializable {
    private static final long serialVersionUID = 1L;
    private String message;
    private static Logger LOGGER=Logger.getLogger("Log");
    public HelloEvent() {
    }

    public HelloEvent(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "helloEvent toString";
    }


}
HelloMessenger
managedbean:

package com.eventhandling;

import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.event.Event;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;


@ManagedBean(name = "messenger")
@SessionScoped
public class HelloMessenger {

    @Inject
    Event<HelloEvent> events;
    private static Logger LOGGER = Logger.getLogger("Log");

    public void hello() {
        LOGGER.info("hello called");
        LOGGER.info("events" + events);
        HelloEvent helloEvent= new HelloEvent("testEvent");
        LOGGER.info("helloEvent"+helloEvent);
        events.fire(helloEvent);

    }

}
JSF视图
Welcome.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body bgcolor="white">
        <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
        <h4>Welcome </h4>
        <h:form>
            <h:commandButton value="Fire Event" action="#{messenger.hello}"></h:commandButton>
        </h:form>
    </h:body>
</html>

您似乎在一个托管bean中混合了JSF和CDI概念

使用
@Named
@SessionScoped
,但请确保您拥有正确的
@SessionScoped
<应删除code>@ManagedBean。javax.enterprise.context是包名。如果使用JSF中的SessionScoped,将发生未记录的行为

我不确定您的依赖关系,但我想id取决于容器。请通过查看启动日志来验证CDI是否启动。确保META-INF中有beans.xml

您粘贴在注释中的日志没有提及Weld或OpenWebBeans,也没有提及您在pom.xml中粘贴的依赖项。我的结论是,您没有正确纳入CDI

你有什么选择?

选项1

Tomcat与OWB/Weld配合得很好,但由于OpenWebBeans与您的容器共享相同的许可证,而且我使用的就是这个许可证,因此我建议您相应地向这些文档添加依赖项:
谢谢你的回复。我做了你提到的改变。并将javax.enterprise.context用于“@SessionScoped”。但是我得到了以下异常:javax.faces.el.EvaluationException:javax.el.PropertyNotFoundException:/welcome.xhtml 14,74 action=“#{messenger.hello}”:目标不可访问,标识符“messenger”解析为null我之前也收到了此异常,因此,我使用了“@ManagedBean”…当我使用“@Named(“messenger”)”[import javax.inject.Named;]时,您能告诉我出现此异常的可能原因吗?您能告诉我如何配置beans.xml吗?因为我后面的示例没有显示任何beans.xml。.在WEB-INF中,您需要beans.xml。该文件可以为空。同样地,您需要在任何jar中使用beans.xml,将其放在META-INF中。谷歌搜索这些信息对您来说应该没有问题。正如您提到的,我已经尝试将beans.xml放在WEB-INF文件夹中。但它仍然不起作用。给出同样的例外。。用谷歌搜索出并将以下bean.xml放在WEB-INF文件夹中,也分别放在META\U INF文件夹中
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body bgcolor="white">
        <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
        <h4>Welcome </h4>
        <h:form>
            <h:commandButton value="Fire Event" action="#{messenger.hello}"></h:commandButton>
        </h:form>
    </h:body>
</html>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.7</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.0</version>
        </dependency>
Jan 30, 2014 4:43:35 PM org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.27 using APR version 1.4
.6.
Jan 30, 2014 4:43:35 PM org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], ra
ndom [true].
Jan 30, 2014 4:43:36 PM org.apache.catalina.core.AprLifecycleListener initialize
SSL
INFO: OpenSSL successfully initialized (OpenSSL 1.0.1d 5 Feb 2013)
Jan 30, 2014 4:43:37 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-apr-6080"]
Jan 30, 2014 4:43:37 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-apr-6006"]
Jan 30, 2014 4:43:37 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 3047 ms
Jan 30, 2014 4:43:37 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 30, 2014 4:43:37 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.42
Jan 30, 2014 4:43:37 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive D:\software\testtomcat_6080-_Analytics_w
ars\apache-tomcat-7.0.42\webapps\JavaServerFaces.war
Jan 30, 2014 4:43:43 PM org.apache.catalina.loader.WebappClassLoader validateJar
File
INFO: validateJarFile(D:\software\testtomcat_6080-_Analytics_wars\apache-tomcat-
7.0.42\webapps\JavaServerFaces\WEB-INF\lib\jsp-api-2.1.jar) - jar not loaded. Se
e Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
Jan 30, 2014 4:43:43 PM org.apache.catalina.loader.WebappClassLoader validateJar
File
INFO: validateJarFile(D:\software\testtomcat_6080-_Analytics_wars\apache-tomcat-
7.0.42\webapps\JavaServerFaces\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded
. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.cl
ass
Jan 30, 2014 4:43:46 PM com.sun.faces.config.ConfigureListener contextInitialize
d
INFO: Initializing Mojarra 2.1.7 (SNAPSHOT 20120206) for context '/JavaServerFac
es'
Jan 30, 2014 4:43:46 PM com.sun.faces.spi.InjectionProviderFactory createInstanc
e
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans metho
ds marked with these annotations will have said annotations processed.
Jan 30, 2014 4:43:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\software\testtomcat_6080-_Analytics
_wars\apache-tomcat-7.0.42\webapps\admin
Jan 30, 2014 4:43:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\software\testtomcat_6080-_Analytics
_wars\apache-tomcat-7.0.42\webapps\docs
Jan 30, 2014 4:43:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\software\testtomcat_6080-_Analytics
_wars\apache-tomcat-7.0.42\webapps\examples
Jan 30, 2014 4:43:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\software\testtomcat_6080-_Analytics
_wars\apache-tomcat-7.0.42\webapps\host-manager
Jan 30, 2014 4:43:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\software\testtomcat_6080-_Analytics
_wars\apache-tomcat-7.0.42\webapps\manager
Jan 30, 2014 4:43:48 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\software\testtomcat_6080-_Analytics
_wars\apache-tomcat-7.0.42\webapps\ROOT
Jan 30, 2014 4:43:48 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-apr-6080"]
Jan 30, 2014 4:43:48 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-apr-6006"]
Jan 30, 2014 4:43:48 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 11372 ms