1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>myDemo</groupId> <artifactId>PromefacesDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>PromefacesDemo Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>prime-repo</id> <name>Prime Repo</name> <url>http://repository.primefaces.org</url> </repository> </repositories> <dependencies> <!-- PrimeFaces --> <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>3.5</version> </dependency> <!-- atmosphere --> <dependency> <groupId>org.atmosphere</groupId> <artifactId>atmosphere-runtime</artifactId> <version>1.1.0.RC2</version> </dependency> <!-- Java EE --> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> <!-- JBoss log --> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.1.3.GA</version> <scope>provided</scope> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>PromefacesDemo</finalName> </build> </project>
2.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Archetype Created Web Application</display-name> <!-- Change to "Production" when you are ready to deploy --> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <!-- JSF mapping --> <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> <!-- Push mapping --> <servlet> <servlet-name>Push Servlet</servlet-name> <servlet-class>org.primefaces.push.PushServlet</servlet-class> <async-supported>true</async-supported> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Push Servlet</servlet-name> <url-pattern>/primepush/*</url-pattern> </servlet-mapping> <!-- Welcome page --> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> </web-app>
3.PushBean.java
每隔1秒push 一次count,持續5秒
package com.jsfdemo.managedbean; import java.io.Serializable; import java.util.concurrent.Delayed; import java.util.logging.Logger; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.primefaces.push.PushContext; import org.primefaces.push.PushContextFactory; @ManagedBean public class PushBean implements Serializable { private static final long serialVersionUID = 1L; Logger logger = Logger.getLogger(this.getClass().getName()); private int count; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public synchronized void increment() { logger.info("increment.."); for (int i = 0; i < 5; i++) { count++; PushContext pushContext = PushContextFactory.getDefault().getPushContext(); logger.info("push count : "+count); pushContext.push("/counter", String.valueOf(count)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
4.PushDemo.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" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:head> <meta charset="utf-8" /> <title>Primefaces push</title> </h:head> <h:body> <h1>Primefaces push..</h1> <h:form id="form"> <h:outputText value="Count : "/> <h:outputText id="out" value="#{pushBean.count}" styleClass="ui-widget display" /> <br /><br /> <p:commandButton value="Click" actionListener="#{pushBean.increment()}" /> </h:form> <p:socket onMessage="handleMessage" channel="/counter" /> <script type="text/javascript"> function handleMessage(data) { console.info(data); $("#form\\:out").html(data); } </script> </h:body> </html>
Demo畫面
沒有留言:
張貼留言