当我尝试在Java EE应用程序中触发事件时,我得到一个空指针异常。下面是代码。
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
托管bean:
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);
}
}
HelloListener
事件监听器:
package com.eventhandling;
import javax.enterprise.event.Observes;
import javax.faces.bean.SessionScoped;
@SessionScoped
public class HelloListener {
public void listenToHello(@Observes HelloEvent helloEvent) {
System.out.println("HelloEvent: " + 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>
Maven依赖项:
<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>
当我点击命令按钮时,我得到一个NullPointerException
。这是因为events
属性未被注入并保持null
。这是怎么造成的,我该如何解决呢?
登录Tomcat启动:
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
答案 0 :(得分:1)
您似乎将JSF和CDI概念混合在一个托管bean中。
使用@Named
和@SessionScoped
但请确保您拥有正确的@SessionScoped
。应删除@ManagedBean
。 javax.enterprise.context是包名。如果使用来自JSF的SessionScoped,则会发生无记录的行为。
我不确定你的依赖关系,但id取决于我猜的容器。请通过查看启动日志来验证CDI启动。确保在META-INF中有beans.xml。
您在评论中粘贴的日志没有提及Weld和OpenWebBeans,也没有提到您在pom.xml中粘贴的依赖项。我的结论是你没有正确地包括CDI。
您有什么选择?
选项1
Tomcat与OWB / Weld相处得很好但是因为OpenWebBeans与你的容器共享相同的许可证,所以我建议你相应地为这些文档添加依赖项: http://openwebbeans.apache.org/owbsetup_ee.html&lt; ---解释您在依赖项中需要什么 http://openwebbeans.apache.org/download.html&lt; ---解释如何添加到pom.xml我写了那些文档,它们还不完美(sry)但它应该让你去。
选项2: 切换到tomEE并仅使用maven中提供的。这是tomEE的版本,包含您需要的所有内容。我真的很喜欢tomEE,这是我的默认推荐。邮件列表非常好,如果您有任何问题,可以立即到达。
选项3: 使用Jboss Weld并按照他们的手册开始使用Tomcat。应该很容易找到使用谷歌。