Reputation: 93
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.
Upvotes: 1
Views: 1158
Reputation: 591
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
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.
Upvotes: 2