【问题标题】:How Primefaces Push <p:socket> work? [duplicate]Primefaces Push <p:socket> 是如何工作的? [复制]
【发布时间】:2014-08-11 14:08:36
【问题描述】:

我正在尝试使用 PF Push Counter 示例,但没有成功。 我有两个 portlet。 一种是计数器portlet。另一个是counterview portlet。 如何订阅(注册)“频道”?

我的代码几乎直接来自 PF Showcase 示例。

1) 计数器(发布者) 计数器.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:body>


    <h:form id="form">
        <h:outputText id="out" value="#{counterBean.count}"
            styleClass="ui-widget display" />

        <p:commandButton value="Click"
            actionListener="#{counterBean.increment}" />
    </h:form>



</h:body>

</html>

CounterBean.java

@ApplicationScoped
@ManagedBean
public class CounterBean {

private volatile int count;

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public void increment() {
    count++;
    System.out.println("increment() " + count);
    EventBus eventBus = EventBusFactory.getDefault().eventBus();
    eventBus.publish("/counter", String.valueOf(count));
}

2) CounterView(消费者) CounterView.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:body>
    <h:form id="form1">
        <h:outputText id="out" value="What's my count?" styleClass="display" />

        <p:remoteCommand name="updateWidgets"
            actionListener="#{consumerBean.printMessage()}" update=":form1:out" />
    </h:form>
    <p:socket onMessage="handleMessage" channel="/counter" />

    <script type="text/javascript">
    function handleMessage(data) {
        updateWidgets();
    }
</script>
</h:body>

</html>

使用这个例子

How Primeface Socket works?

CounterResource.java

@PushEndpoint("/counter")
public class CounterResource {

    @OnMessage(encoders = { JSONEncoder.class })
    public String onMessage(String count) {
        System.out.println("OnMessage " + count );
        return count;
    }
}

@SessionScoped
@ManagedBean
public class ConsumerBean {
private int count;
private String message;


public int getCount() {
    return count;
}
public void setCount(int count) {
    this.count = count;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}

public void printMessage(){
    System.out.println("Consumer Bean Listener!");

}



}

问:count.xhtml 上的计数器增加,但 counterView.html 上没有任何反应。 我在 Consumer bean 中设置了几个断点,但没有任何停止。

我怎样才能完成这项工作? 这似乎是一个简单的示例,但无法使其工作。 你能帮忙吗?

【问题讨论】:

  • PrimeFaces 自 PrimeFaces 6.3 起被移除。不要使用它。通过 使用 JSF 2.3 本机推送。或者,如果您仍在使用 JSF 2.2,请使用 OmniFaces

标签: jsf primefaces push


【解决方案1】:

1st:你的 ConsumerBean 是做什么用的?您有一个应用程序范围的 CounterBean,其中包含您希望通过“推送”显示的计数器。那么为什么在 ConsumerBean 中有另一个计数器呢?

第二个:在 CounterView.xhtml 中你根本不显示计数器,甚至是 ConsumerBean 的计数器。

在您的 CounterView.xhtml 页面中使用 CounterBean(及其计数器)。 在 h:outputText 而不是“我的计数是多少?”固定值使用#{counterBean.count}(与counter.xhtml中的方式相同)。

顺便说一下,查看官方演示,它运行良好: http://www.primefaces.org/showcase/push/counter.xhtml

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 2011-10-03
    • 2016-01-19
    • 2014-08-25
    • 2011-10-02
    • 2011-10-22
    相关资源
    最近更新 更多