在仍在方法内运行时渲染或更新页面

时间:2015-02-20 06:36:36

标签: jsf-2 rendering

我想知道你是否可以在方法内部渲染或更新页面; 这是一个例子: .xhtml文件:

<h:form id="form">
    <h:commandButton value="change" actionListener="#{bean.changeText}">
        <f:ajax render="out" />
    </h:commandButton>
    <h:outputLabel id="out" value="#{bean.text}" />
</h:form>

这是changeText方法:

public void changeText() throws InterruptedException{
    text = "test1"; 
    FacesContext.getCurrentInstance().renderResponse();
    Thread.sleep(2000);
    text = "test2";
}

现在可以将输出标签(id = out)设置为渲染(或更改)为“test1”,然后在2秒后它变为“test2”? 看看JSF生命周期,我认为这是不可能的,但也许我的生命周期错了,或者你们其中一个人知道一个解决方法。

1 个答案:

答案 0 :(得分:1)

不,那是不可能的。渲染响应仅在操作方法返回时开始。

基本上有两种方法可以实现所需的行为:轮询或推送。根据您的问题历史记录,您使用的是Java EE 7 + JSF 2.2 + PrimeFaces。您可以使用Java EE 7的新WebSocket API(JSR-356)推送使用PrimeFaces <p:socket>或homegrow。