问题
I need a push
functionality but this simple code not working: http://www.primefaces.org/showcase/push/counter.xhtml
I need some specific library?
The next code is the mine; the same of the primefaces page.
CounterResource.java:
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.impl.JSONEncoder;
@PushEndpoint("/counter")
public class CounterResource {
@OnMessage(encoders = {JSONEncoder.class})
public String onMessage(String count) {
return count;
}
}
GlobalCounterView.java
import java.io.Serializable;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.push.EventBus;
import org.primefaces.push.EventBusFactory;
@ManagedBean
@ApplicationScoped
public class GlobalCounterView implements Serializable{
private volatile int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void increment() {
count++;
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/counter", String.valueOf(count));
}
}
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>
<h:form id="form">
<h:outputText id="out" value="#{globalCounterView.count}" styleClass="ui-widget display" />
<p:commandButton value="Click" actionListener="#{globalCounterView.increment}" />
</h:form>
<p:socket onMessage="handleMessage" channel="/counter" />
<script type="text/javascript">
function handleMessage(data) {
$('.display').html(data);
}
</script>
</h:body>
</html>
My 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>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Please, help me. Thank you!
回答1:
PrimeFaces Push was removed in 6.3 you can see in this ticket: https://github.com/primefaces/primefaces/issues/3385
You can read why PrimeFaces decided to remove it: https://www.primefaces.org/primefaces-6-2-roadmap/
PrimeFaces Push uses atmosphere framework under the hood and since JSF 2.3 provides a socket component, we believe it is time to retire Push. As with mobile, push will be deprecated in 6.2 and removed in 6.3.
In summary, we’d like to focus and spend our time more on the core components and features.
So your code must be referencing Push which explains the Class Not Found error using 6.3-SNAPSHOT.
回答2:
First, you need to add atmospheric dependencies to the project.
pom.xml
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime-native</artifactId>
<version>2.4.3</version>
</dependency>
Then you will have to edit your web.xml file as follows. web.xml
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
<init-param>
<param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
<param-value>org.atmosphere.cache.UUIDBroadcasterCache</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/primepush/*</url-pattern>
</servlet-mapping>
I use it this way in my project and it works fine.
来源:https://stackoverflow.com/questions/41194581/how-can-i-do-working-push-of-primefaces-6-0