jsf 2 - How to avoid preRenderView method call in ajax request? -


i need call method in backing bean while page loads. achieved using

<f:event listener="#{managedbean.onload}" type="prerenderview"> 

but whenever ajax request made in page, method invoked again. don't need in requirement. how avoid method call in ajax request?

the prerenderview event invoked on every request before rendering view. ajax request request renders view. behavior expected.

you've 2 options:

  1. replace @postconstruct method on @viewscoped bean.

    @managedbean @viewscoped public class managedbean {      @postconstruct     public void onload() {         // ...     }  } 

    this invoked when bean constructed first time. view scoped bean instance lives long you're interacting same view across postbacks, ajax or not.


  2. perform check inside listener method if current request ajax request.

    @managedbean // scope. public class managedbean {      public void onload() {         if (facescontext.getcurrentinstance().getpartialviewcontext().isajaxrequest()) {              return; // skip ajax requests.         }          // ...     }  } 

    or, if you're interested in skipping postbacks instead of ajax requests, instead:

            if (facescontext.getcurrentinstance().ispostback()) {              return; // skip postback requests.         } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -