在@ApplicationScope的ManagedBean中使用ajax进行轮询

时间:2021-08-29 20:04:13

I have trouble understanding the operation of the @ApplicationScope when the request is made by Ajax.

当Ajax发出请求时,我无法理解@ApplicationScope的操作。

I created a small example to facilitate understanding. Where I have:

我创建了一个小例子来促进理解。我在哪里:

  • A slider changes a variable called sliderValue in a ManagedBean of @ApplicationScope, the value is set by Ajax.
  • 滑块在@ApplicationScope的ManagedBean中更改名为sliderValue的变量,该值由Ajax设置。

  • A poll which is updating the panelGrid to always get the updated value of sliderValue, the update is done by Ajax.
  • 正在更新panelGrid以始终获取sliderValue的更新值的轮询,更新由Ajax完成。

Theoretically all users from accessing this page, should have the same value for sliderValue, and if a user changes the value of the slider, all others should receive the change, right?

从理论上讲,访问此页面的所有用户都应该具有相同的sliderValue值,如果用户更改了滑块的值,其他所有用户都应该收到更改,对吧?

But this does not occur. Apparently, when the update is made via Ajax, he is behaving like a ManagedBean of @SessionScope.

但这不会发生。显然,当通过Ajax进行更新时,他的行为类似于@SessionScope的ManagedBean。

When I change the value of the sliderValue, it is being changed in ManagedBean correctly, but other users do not receive the update via the update performed bypoll.

当我更改sliderValue的值时,它正在ManagedBean中正确更改,但其他用户不会通过bypoll执行的更新来接收更新。

I can only update the sliderValuevalue if I give a REFRESH in the browser and do a full REFRESH the page.

如果我在浏览器中提供REFRESH并在页面上执行完整的REFRESH,我只能更新sliderValuevalue。

Has anyone experienced similar problem?

有没有人遇到类似的问题?

index.xhtml

<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>Menu</title>
    </h:head>
    <h:body>
        <h:form prependId="false" id="form1" >

            <h:panelGrid id="panelGrid1"  columns="1" style="margin-bottom: 10px">
                <p:inputText id="txt1" value="#{menuManagedBean.sliderValue}" />
                <p:slider id="slider1" for="txt1" >
                    <p:ajax event="slideEnd" process="txt1" />
                </p:slider>
            </h:panelGrid>

            <p:poll id="poll1" widgetVar="varPool1" async="true" autoStart="true" interval="2" update="panelGrid1" />

        </h:form>
    </h:body>
</html>

MenuManagedBean.java

import java.io.Serializable;
import java.util.Date;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@ApplicationScoped
@Named(value = "menuManagedBean")
public class MenuManagedBean implements Serializable {

    private int sliderValue;

    public MenuManagedBean() {
    }

    public int getSliderValue() {
        System.out.println(new Date() + " - get: " + sliderValue);
        return sliderValue;
    }

    public void setSliderValue(int sliderValue) {
        this.sliderValue = sliderValue;
        System.out.println(new Date() + " - set: " + sliderValue);
    }
}

1 个解决方案

#1


5  

The <p:poll> submits/processes by default the entire form as in <p:poll process="@form">. Including the current slider value. You should have noticed it by an unnecessary set method call. Every open view submits its own current slider value during the poll. That's why every open view only gets to see its own slider value (leaving race conditions outside consideration when there are "many" open views).

默认提交/处理整个表单,如 。包括当前滑块值。您应该通过不必要的set方法调用注意到它。每个打开的视图在轮询期间提交其自己的当前滑块值。这就是为什么每个开放视图只能看到自己的滑块值(当有“很多”打开视图时,不考虑竞争条件)。

Tell <p:poll> to only process itself, not the entire form.

告诉 只处理自己,而不是整个表单。

<p:poll process="@this" ... />

Unrelated to the concrete problem: don't use prependId="false" ever. Get rid of it.

与具体问题无关:不要使用prependId =“false”。摆脱它。

#1


5  

The <p:poll> submits/processes by default the entire form as in <p:poll process="@form">. Including the current slider value. You should have noticed it by an unnecessary set method call. Every open view submits its own current slider value during the poll. That's why every open view only gets to see its own slider value (leaving race conditions outside consideration when there are "many" open views).

默认提交/处理整个表单,如 。包括当前滑块值。您应该通过不必要的set方法调用注意到它。每个打开的视图在轮询期间提交其自己的当前滑块值。这就是为什么每个开放视图只能看到自己的滑块值(当有“很多”打开视图时,不考虑竞争条件)。

Tell <p:poll> to only process itself, not the entire form.

告诉 只处理自己,而不是整个表单。

<p:poll process="@this" ... />

Unrelated to the concrete problem: don't use prependId="false" ever. Get rid of it.

与具体问题无关:不要使用prependId =“false”。摆脱它。

相关文章