在方法中仍然 运行 呈现或更新页面

Render or update a page while still running inside a method

我想知道您是否可以在仍在方法中时呈现或更新页面; 这是一个例子: .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 生命周期,我认为这是不可能的,但也许我弄错了生命周期,或者你们中的某个人知道解决方法。

不,那不可能。渲染响应仅在操作方法 returns.

后启动

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

自己开发一个