我正在尝试http://www.primefaces.org/showcase/push/notify.xhtml
中的通知示例我正在使用primefaces 5和glassfish 4。
但是,我的onMessage
代码未被调用。
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
我的index.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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<p:growl widgetVar="growl" showDetail="true" />
<h:form>
<h:panelGrid columns="2">
<p:outputLabel for="summary" value="Summary: " />
<p:inputText id="summary" value="#{notifyView.summary}" required="true" />
<p:outputLabel for="detail" value="Detail: " />
<p:inputText id="detail" value="#{notifyView.detail}" required="true" />
</h:panelGrid>
<p:commandButton value="Send" actionListener="#{notifyView.send}" />
</h:form>
<p:socket onMessage="handleMessage" channel="/notify" />
<script type="text/javascript">
function handleMessage(facesmessage) {
facesmessage.severity = 'info';
PF('growl').show([facesmessage]);
}
</script>
</h:body>
</html>
My NotifyView.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.primefaces.showcase.push.notify;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.push.EventBus;
import org.primefaces.push.EventBusFactory;
@ManagedBean
@RequestScoped
public class NotifyView {
private final static String CHANNEL = "/notify";
private String summary;
private String detail;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public void send() {
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish(CHANNEL, new FacesMessage(StringEscapeUtils.escapeHtml3(summary), StringEscapeUtils.escapeHtml3(detail)));
System.out.println("Send");
}
}
My NotifyResource.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.primefaces.showcase.push.notify;
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) {
System.out.println("onMessage");
return message;
}
}
我图书馆中的所有文件
我已经包含一个println("onMessage")
来检查是否调用了NotifyResource.java中的@OnMessage。
出于某种原因,NotifyResource.java中的@OnMessage没有被调用。
为什么会这样呢?
答案 0 :(得分:0)
你需要一个创建FaceMessage对象的解码器: - )
由于
== Jeanfrancois
答案 1 :(得分:0)
将以下脚本移至<head>
<script type="text/javascript">
function handleMessage(facesmessage) {
facesmessage.severity = 'info';
PF('growl').show([facesmessage]);
}
</script>