是否在会话范围内的JSF支持bean中观察到CDI事件 [英] Do CDI Events observed across session scoped JSF backing beans

查看:70
本文介绍了是否在会话范围内的JSF支持bean中观察到CDI事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过多个JSF 2.0会话作用域后备bean来观察CDI事件.我以为可以通过观察事件将事件/数据推送到多个会话.

I'm wondering if it is possible to observe a CDI event with multiple JSF 2.0 session scoped backing beans. I thought that I could push event/data to multiple sessions by observing the event.

我已经建立了一个小测试,允许用户使用页面上的按钮来触发事件(该按钮与会话范围的后备Bean中实际触发事件的方法相关).我以为如果打开两个不同的浏览器,将创建两个会话,并且该事件将通知每个会话范围内的支持bean.

I've set up a small test that allows the user to fire the event using a button on the page (which is tied to a method in the session scoped backing bean that actually fires the event). I thought that if I opened two different browsers, two sessions would be created and the event would notify each of the session scoped backing beans.

但是,当运行我的小测试并单击按钮以在其中一个浏览器上触发事件时,我看到该事件仅通知一个会话范围的bean.它仅通知从中触发事件的bean(即,如果我单击浏览器1中的按钮,则通知在浏览器1中支持会话的会话范围的bean,并且如果我单击浏览器2中的按钮,则支持该会话的bean.浏览器2收到通知).

However, when running my little test and clicking the button to fire the event on one of the browsers, I see that the event only notifies one of the session scoped beans. It only notifies the bean from which the event was fired (i.e. - if I click the button in browser 1 the session scoped bean backing the session in browser 1 is notified, and if I click the button in browser 2 the bean backing the session in browser 2 is notified).

我的印象是事件将通知所有bean实例.但是,事实并非如此.我应该能够做到这一点吗?我只是设置错误了吗?

I was under the impression that events would notify all bean instances. However, this doesn't seem to be the case. Should I be able to do this? Do I just have something setup wrong?

更新以显示我的代码如下:

UPDATE to show what my code looks like:

一个jsfpage.xhtml片段,以触发一个事件并显示会话范围的数据:

A snippet of jsfpage.xhtml to fire an event and show the session-scoped data:

<h:form>
  <h:outputText value="#{BackingBean.property}" />
  <p:commandButton value="Fire Event" action="#{EventFirer.fireEvent}" />
</h:form>

接收事件的Session范围的bean:

The Session-scoped bean that receives event:

@Named
@SessionScoped
public class BackingBean implements Serializable {

    private String property;

    public String getProperty() {
        return property
    }

    public void listenForChange(@Observes EventObj event) {
        logger.log(Level.INFO, "Event received");
        property = event.toString();
    }
}

一个应用程序范围的bean来触发事件:

An application scoped bean to fire the event:

@Named
@ApplicationScoped
public class EventFirer implements Serializable {

    @Inject
    private Event<EventObj> events;

    public String fireEvent() {
        logger.log(Level.INFO, "Event fired");
        events.fire(new EventObj());
        return null;
    }
}

推荐答案

首先,您最好指定事件的类型:

First, you'd better specify the type of the event:

@Inject
private Event<EventObj> events;

此外,规范中没有任何指示会限制在其上调用观察者方法的bean实例.我会为此提出一个问题(在您使用的实现的Bugtracker中.也许是Weld?)

Apart from that there is no indication in the spec that would limit the bean instances on which the observer method is invoked. I'd file an issue about this (in the bugtracker of the implementation you are using. Perhaps Weld?)

这篇关于是否在会话范围内的JSF支持bean中观察到CDI事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆