Polling with ajax in ManagedBean from @ApplicationScope
I'm having a hard time understanding how it works @ApplicationScope
when the Ajax request is made.
I created a small example to make it easier to understand. Where I am:
- A
slider
modifies the variable namedsliderValue
in the ManagedBean@ApplicationScope
, the value is set by Ajax. - A
poll
that updatespanelGrid
to always get the updated valuesliderValue
, the update is done by Ajax.
In theory, all users accessing this page should have the same value for sliderValue
, and if the user changes the value of the slider, everyone else should receive the change, right?
But that doesn't happen. Apparently when the update is done via Ajax, it behaves like a ManagedBean @SessionScope
.
When I change the value sliderValue
, it changes in the ManagedBean correctly, but other users are not getting the update via the update done with poll
.
I can update the sliderValue
if I give REFRESH in the browser and make a full REFRESH page.
Has anyone faced a 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);
}
}
source to share
<p:poll>
submits / processes the whole form by default, as in <p:poll process="@form">
. Including the current value of the slider. You should have noticed this with an unnecessary method call set
. Each open view passes its current slider value during polling. Therefore, each open view only gets its own slider value (leaving race conditions out of the question when there are "many" open views).
Tell <p:poll>
only to handle yourself, not the entire form.
<p:poll process="@this" ... />
Unrelated to a specific problem: don't use prependId="false"
ever. Get rid of it.
source to share