2

PrimeFaces 5.0, JSF 2.2, Glassfish 4.1.1,

I'm assuming that something in my setup isn't configured correctly but I'm not sure what to look at...

So I'm using managedbeans to back JSF pages. Within the PrimeFace elements on the page if I use an oncomplete property the method it refers to gets called on the page load. I don't want it to call the method on each page load and i don't believe it should be!

I have tested this through several pages and within different elements. I first noticed this issue with a <p:fileupload> element where I was trying to call a method, using oncomplete, once all the files had been uploaded. I have subsequently tried this on <p:commandbuttons> and the oncomplete is called when the page is loaded.

I have checked the resulting HTML and there is a properly formatted <head> tag pair.

I have also completed a diff on the generated HTML with and without the oncomplete property present.

With:

<button id="j_idt21:j_idt26" 
    name="j_idt21:j_idt26" 
    class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
    onclick="PrimeFaces.ab({s:'j_idt21:j_idt26',onco:function(xhr,status,args){;}});
    return false;" 
    type="submit">`

Without:

<button id="j_idt21:j_idt26" 
    name="j_idt21:j_idt26" 
    class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" 
    onclick="PrimeFaces.ab({s:'j_idt21:j_idt26'});
    return false;" 
    type="submit">

Here is the code for the above HTML showing that oncomplete invokes a method on any page when it is loaded. This is my login page.

<p:commandButton validateClient="true" value="Login" 
    action="#{loginJSFManagedBean.validateCredentials}" 
    type="submit" 
    ajax="true" 
    oncomplete="#{loginJSFManagedBean.validateCredentials}">
</p:commandButton>

Each time the login page is loaded the validateCredentials method is called, even before the page has finished rendering...

here is an snippet of my JSF for my file upload:

<h:form enctype="multipart/form-data">
<p:fileUpload oncomplete="#{JSFManagedBean.extract()}" 
fileUploadListener="#{JSFManagedBean.handleTarFileUpload}" 
mode="advanced"  ></p:fileUpload>
</h:form>

Each time the page loads the extract() method is called...

Trig
  • 21
  • 1
  • 3
  • 1
    Where in the documentation does it state that the 'oncomplete' is capable of calling a java serverside method when 'exectuted'? – Kukeltje May 05 '17 at 13:15
  • Possible duplicate of [How to call JSF backing bean method only when onclick event occurs](http://stackoverflow.com/questions/8705614/how-to-call-jsf-backing-bean-method-only-when-onclick-event-occurs) – Kukeltje May 05 '17 at 13:17
  • What you're trying to do on the button might be the job for first an actionListener and then an action. For the fileupload you can do http://stackoverflow.com/questions/26696922/finish-uploading-files-and-then-execute-the-jsfaction-of-the-button or http://stackoverflow.com/questions/20747201/when-multiple-upload-is-finished-in-pfileupload – Jaqen H'ghar May 06 '17 at 07:01

1 Answers1

2

oncomplete is a client-side javascript callback. Your methods get called when the page is rendered, because the server evaluates those EL expressions expecting them to result in strings with javascript code to send.

You probably don't need these oncomplete handlers at all. For the login button the action handler should be sufficient. For the fileUpload the fileUploadListener may be sufficient, if your goal is simply to "extract" the uploaded file as the method name suggests.

Vsevolod Golovanov
  • 4,068
  • 3
  • 31
  • 65
  • There are lots of 'duplicates' about this, I even referenced on. It is gopd practice to mark this question as duplicate then so StackOverflow won't be littered with almost identical answers – Kukeltje May 07 '17 at 13:26