preRenderView is called on every ajax request

北战南征 提交于 2019-12-05 01:13:18

问题


I am implementing infinite scrolling using jquery waypoints and jsf following link . I have prerender for one of the xhtml on which infinite scrolling is required . Now, as waypoint sends ajax request so why for every scroll it is calling prerender that means whole page is getting refeshed. Please let me know how to solve this.


回答1:


You seem to think that the preRenderView event is invoked only once during the construction of the view and not invoked on subsequent requests on the same view. This is untrue. The preRenderView event is invoked right before rendering of the view. The view is rendered on every request. This also includes ajax requests (how else should it produce the necessary HTML output for ajax requests?). So the behavior which you're seeing is fully expected. You was simply using the wrong tool for the job.

You should either be using the @PostConstruct method of a @ViewScoped bean,

@ManagedBean
@ViewScoped
public class Bean {

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

    // ...
}

or be adding a negation check on FacesContext#isPostback() in the pre render view event listener

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

See also:

  • When to use f:viewAction / preRenderView versus PostConstruct?


来源:https://stackoverflow.com/questions/19974950/prerenderview-is-called-on-every-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!