PrimeFaces Push counter(sayıcı)-Apache Tomcat-Eclipse kodu kullanımı örneği (PrimeFaces Push counter-Epache Tomcat-Eclipse code usage example)

PrimeFaces Push ile sunucu katmanındaki olay bildiricisini dinleyerek olay bildiricisinden gelen bilgiye göre ekrandaki bilgileri güncelleme veyahutta başka işlemleri yapmaya olanak sağlar. Maven kullanmadan Eclipse ve Apache Tomcat ile PrimeFaces Push örneğinin yapısını oluşturmak için aşağıdaki adımları yapabilirsiniz.

Eclipse
File-> New-> Other-> Web-> Dynamic Web Project-> Next->
->
Target runtime ->
Dynamic web module version <3.0>->
Configuration ->
Next-> Next-> Next->
Manages Libraries-> Import-> ->
Next-> Finish->

Web Content-> WEB-INF-> lib->
primefaces-5.0.jar
atmosphere-runtime-2.2.4.jar
slf4j-api-1.6.1.jar
slf4j-nop-1.7.9.jar
javax-inject.jar

Web Content-> WEB-INF-> web.xml->

<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>

Web Content-> counter.xhtml->

<!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></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>

src->net.yazilimcity-> CounterResource.java

package net.yazilimcity;

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;
    }
}      

src->net.yazilimcity-> GlobalCounterView.java

package net.yazilimcity;

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));
    }
}

counter.xhtml-> Run-> Run-> Next-> Finish

Share on Facebook0Tweet about this on TwitterShare on Google+0Share on StumbleUpon0Share on Reddit0

Leave a Reply


*