每个ajax请求都会调用preRenderView

Rah*_*ngh 4 ajax jsf primefaces infinite-scroll prerenderview

我正在使用jquery waypoints和jsf跟随链接实现无限滚动.我有一个xhtml的prerender,需要无限滚动.现在,当waypoint发送ajax请求时,为什么每个滚动它调用prerender,这意味着整个页面都被重新刷新.请让我知道如何解决这个问题.

Bal*_*usC 13

您似乎认为preRenderView在构建视图期间仅调用该事件一次,而在同一视图上的后续请求中不调用该事件.这是不真实的.preRenderView在渲染视图之前调用该事件.视图在每个请求上呈现.这还包括ajax请求(如何为ajax请求生成必要的HTML输出?).所以你所看到的行为是完全可以预期的.你只是使用错误的工具来完成工作.

你应该使用bean 的@PostConstruct方法@ViewScoped,

@ManagedBean
@ViewScoped
public class Bean {

    @PostConstruct
    public void init() {
        // Do here your thing during construction of the view.
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

或者FacesContext#isPostback()在预渲染视图事件监听器中添加否定检查

public void preRender() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // Do here your thing which should run on initial (GET) request only.
    }
}
Run Code Online (Sandbox Code Playgroud)

也可以看看: