Jsf 素数动态间隔

Jsf 素数动态间隔,jsf,primefaces,jsf-2.2,Jsf,Primefaces,Jsf 2.2,我编写了如下示例代码: @ViewScoped @Named public class SampleBean implements Serializable { private int interval = 1; public void execute() { switch (interval) { case 1: System.out.println("1 Executed ...");

我编写了如下示例代码:

@ViewScoped
@Named
public class SampleBean implements Serializable {

    private int interval = 1;


    public void execute() {
        switch (interval) {
            case 1:
                System.out.println("1 Executed ...");
                break;
            case 2:
                System.out.println("2 Executed ...");
                break;
            case 3:
                System.out.println("3 Executed ...");
                break;
        }
    }

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }
}    
这个xhtml:

<h:form>
        <p:selectOneButton value="#{sampleBean.interval}">
            <f:selectItem itemLabel="1" itemValue="1"/>
            <f:selectItem itemLabel="2" itemValue="2"/>
            <f:selectItem itemLabel="3" itemValue="3"/>
        </p:selectOneButton>

        <p:poll interval="#{sampleBean.interval}" listener="#{sampleBean.execute}" id="poll"/>

    </h:form>    

我想动态更新primefaces轮询组件的间隔时间,但不使用示例代码进行更改。睡眠始终每1秒执行一次。

我的代码有什么问题

您需要告诉
p:poll
组件重新提取其支持的bean值。通过在组件上调用新的渲染阶段(通过PrimeFaces
update
或通过
f:ajax
render
)可以轻松完成此任务

所以要让它发挥作用,你只需要做这样的事情

<h:form>
    <p:selectOneButton value="#{sampleBean.interval}">
        <f:selectItem itemLabel="1" itemValue="1"/>
        <f:selectItem itemLabel="2" itemValue="2"/>
        <f:selectItem itemLabel="3" itemValue="3"/>
        <f:ajax render="@form"/>
    </p:selectOneButton>
    <p:poll interval="#{sampleBean.interval}" listener="#{sampleBean.execute}" id="poll"/>
</h:form>    

注意上面的
f:ajax
调用


您需要以封闭(父)组件为目标,以使其刷新-这就是为什么我们以表单为目标,而不是直接以轮询组件为目标。

谢谢adam:如何仅按id呈现轮询组件?我使用render=“poll”但收到错误!Found:在窗体上设置id并使用No,这将更新窗体的子级。指定
render=“myForm poll”
告诉ajax标记(和JSF)刷新
myForm
poll
的子项,因此在这个特定实例中,它与指定
@form
相同。您以组件的子组件为目标-因此,如果您只想刷新
p:poll
,解决方案是将其包含在一个组件中,并以该组件为目标进行刷新(如答案中所述)。