2017-01-10 59 views
0

我遇到PrimeFaces 6.0推送問題,我在這裏搜索了所有問題,沒有答案幫助我。我建立一個內部郵件系統,我想通知用戶,如果他有基於後端事件(觀察者模式)插在DB新的電子郵件,這裏是我的代碼:PrimeFaces Push EventBusFactory.getDefault()。eventBus()返回null

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 
    <display-name>Test Foo Email system</display-name> 
    <servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <!-- primeFaces push declaration --> 
    <servlet> 
    <servlet-name>Push Servlet</servlet-name> 
    <servlet-class>org.primefaces.push.PushServlet</servlet-class> 
    <async-supported>true</async-supported> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Push Servlet</servlet-name> 
    <url-pattern>/primepush/ *</url-pattern> 
</servlet-mapping> 
<!-- Ends here--> 
    <servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/ *</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>client</param-value> 
    </context-param> 
    <context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>resources.application</param-value> 
    </context-param> 
    <listener> 
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
    </listener> 
</web-app> 

我foo.xhtml頁面代碼示例

<script type="text/javascript"> 
    function handleMessage(facesmessage) { 
     facesmessage.severity = 'info'; 

     PF('growl').show([facesmessage]); 
    } 
</script> 
<div class="New_Message_container "> 
      <!-- primefaces Push component --> 
      <p:growl widgetVar="growl" showDetail="true" /> 
      <p:socket onMessage="handleMessage" channel="/notify" /> 
      </div> 

控制器類:

private final static String CHANNEL = "/notify"; 
. 
. 
. 
private void notifyView(NewEmailEvent evt) { 
    String CurrentUser = this.currentUser.getEmailID(); 
    String EventUserId = evt.getUserID(); 
    if (CurrentUser.equalsIgnoreCase(EventUserId)) { 
    EventBus eventBus = EventBusFactory.getDefault().eventBus(); 
    eventBus.publish(CHANNEL, 
    new FacesMessage("New Mail", "You have new Mail")); 
    } 

} 

NotifyResource類

import javax.faces.application.FacesMessage; 
import org.primefaces.push.annotation.OnMessage; 
import org.primefaces.push.annotation.PushEndpoint; 
import org.primefaces.push.impl.JSONEncoder; 

@PushEndpoint("/notify") 
    public class NotifyResource { 

@OnMessage(encoders = {JSONEncoder.class}) 
    public FacesMessage onMessage(FacesMessage message) { 
     return message; 
    } 

} 

,我發現這是需要這種代碼中包含了需要的庫,我已經下載從中我的示例中使用的網絡,這些罐子。

atmosphere-annotations-2.4.9.jar 
atmosphere-compat-jbossweb-2.0.1.jar 
atmosphere-compat-tomcat-2.0.1.jar 
atmosphere-compat-tomcat7-2.0.1.jar 
atmosphere-runtime-2.4.9.jar 
primefaces-6.0.jar 
slf4j-api-1.7.22.jar 
slf4j-simple-1.7.22.jar 

我已經厭倦了調試我的代碼和我注意到,事件總線它NULL當我嘗試使用它(eventBus.publish()),所以我就顯示java.lang.NullPointerException

+0

Stackoverflow中的幾個'重複'。請嘗試搜索它們。右側的「相關」列已經顯示其中一些 – Kukeltje

+0

您好kukeltje,感謝您的評論,不幸的是我已經嘗試了所有發佈的答案,但我無法弄清楚我在尋找這個問題的問題。現在一週,我也是jsf的初學者。 –

+0

什麼是服務器運行時間? Tomcat的?試圖添加cdi作爲一個庫 – Kukeltje

回答

0
<servlet-mapping> 
    <servlet-name>Push Servlet</servlet-name> 
    <url-pattern>/primepush/ *</url-pattern> 
</servlet-mapping> 

/primepush /和*之間有空格,請嘗試刪除它。