Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preRenderView is called on every ajax request

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.

like image 782
Rahul Singh Avatar asked Dec 12 '22 09:12

Rahul Singh


1 Answers

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?
like image 121
BalusC Avatar answered Mar 09 '23 00:03

BalusC