0

I've got a minimal, complete, and verifiable example working as expected, on which there is an issue I don't finish to understand. The code can be seen below and its behaviour basically consists of selecting an item from a list of a PF ajaxified <p:selectOneListbox> and displaying the item's value on a JSF <h:outputText> element.

<h:form id="myform">
    <p:selectOneListbox id="myselect" value="#{bean.optionSelected}">
        <p:ajax listener="#{bean.onChange}" process="myselect" update="toupdate" onstart="onstart()" oncomplete="oncomplete()" onerror="onerror()" onsuccess="onsuccess()"/>
        <f:selectItem itemLabel="Option 1" itemValue="1" />
        <f:selectItem itemLabel="Option 2" itemValue="2" />
        <f:selectItem itemLabel="Option 3" itemValue="3" />
    </p:selectOneListbox>    

    <h:outputText id="toupdate" value=">#{bean.optionSelected}" />
</form>

Looking at the element, I don't just know what specific event is causing the ajax request to be sent to the server, that is, I don't know if the triggered event was the valuechange event or some other. In other words, I miss a <p:ajax> element coded in this way:

<p:ajax event="name_of_the_event" .../>

And this doubt makes me to not know the class of the receiving event to be used by the listener method in the backing bean side: public void onChange(??? event)

Any clarification/explanation would be really appreciated. Thanks.

txapeldot
  • 71
  • 2
  • 10
  • It is common/good practice that if an answer is correct and helped, to at least 'accept' and if it was more than a little useful, also upvote it. – Kukeltje Jan 16 '18 at 20:41

1 Answers1

1

According to the PrimeFaces dropdown showcase <p:ajax listener="..." /> inside a <p:selectOneListbox /> will call the listener when the user selects a different item (onchange).

And the ajax tag documentation says that the event attribute is optional and:

Client side event to trigger ajax request. Default value is defined by parent ClientBehaviorHolder component the behavior is attached to.

The listener in the p:ajax tag does not need to contain any parameters if you want to

  • call a method with the default event as parameter
  • call a method without any parameter

So <p:ajax listener="#{myBean.onAjaxAction}" />

Would call

public void onAjaxAction(){
  ...
}

If there is no other method with a more matching method signature.

If you want to have more info about the event, you can leave the EL identival and server-side add an event. Each concrete event as a parameter should extend javax.faces.event.AjaxBehaviorEvent.

public void onAjaxAction(javax.faces.event.AjaxBehaviorEvent event) {
  System.out.println(event.getClass())
}

You can then retrieve the event source amongst other things. If you need even more info (if available) you need to use a more concrete event class. If you do not know what the concrete default event (and its corresponding class) is, you can add the method above and in the method try to inspect the concrete event (as is done in the System.out...)

For many PrimeFaces components the events are mentioned in the documentation. And all existing PrimeFaces events (for all components) can be found in the org/primefaces/event package so using code-completion in an IDE would give you options (mind that not all work in all components obviously).

If there are no explicitly named events in the documentation, the basic dom events should at least work, including the onchange. Most often for inputs 'onchange' is the default event.

Since for the <p:selectOneListbox /> there are no explicit events mentioned in the docs, the default event to trigger ajax request will be onchange. For this the javax.faces.event.AjaxBehaviorEvent should be used as parameter if needed.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • 1
    And please point to a much more recent tag documentation... (edited for you) – Kukeltje Jan 10 '18 at 19:50
  • 1
    The fact that the method is called onchange shoud not be taken that the real Ajax event is called the same. And you don't go into what the event in the method signature could (should???) be. I'll add that too – Kukeltje Jan 10 '18 at 20:10
  • Thanks for the extensive explanation. Just two things. 1) Official documentation on **SelectOneListBox** describes `valueChangeListener` as a method expression for handling a _valuechangeevent_. So, what is the right name of the event? is it _onchange_ or _valuechange_? And 2) Within the provided link where "all existing PrimeFaces events (for all components) can be found", it doesn't seem to appear any event related to the _change_ event of the `` element. – txapeldot Jan 11 '18 at 07:39
  • @Txapeldot: Read https://stackoverflow.com/questions/11879138/when-to-use-valuechangelistener-or-fajax-listener (hint: find all >50 upvotes JSF Q/A, scan them and remember their existence. Many of them will come handy! – Kukeltje Jan 11 '18 at 12:15
  • @txapeldot: 2: the onchange is a standard DOM event and not a PrimeFaces specific one (just like click, focus, blur etc) so there is not any additional class for it) – Kukeltje Jan 11 '18 at 12:23