I am using JSF in my project. I am using a context menu from PrimeFaces. I see in p:menuItem
we have action
, actionListener
, onclick
methods. So my question is: When do I have to use action
, actionListner
, onclick
and what is the order of execution?

- 5,276
- 4
- 30
- 41

- 34,250
- 42
- 115
- 150
-
At least a partial answer: http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener – Ian McLaird Jan 29 '14 at 17:58
-
Also see: Other part: http://stackoverflow.com/a/9115951/757071 – Johny T Koshy Jan 29 '14 at 18:02
2 Answers
onclick
will be executed first. It is used to call a javascript function.actionListener
is used when you want to have some ajax call to a
method. That method should have the return typevoid
, the method either take anActionEvent
as argument or no argument; it can also be used for a non-ajax call but then the page will be refreshed.action
is used to navigate to a different page; the method should have the return typeString
.

- 8,008
- 26
- 77
- 177

- 429
- 3
- 10
This question has been asked before. Action is used when you want to call a method in your backing bean. e.g
action="#{myBean.myMethod}"
the code for bean would be like
@ManagedBean(name = "myBean", eager = true)
@ViewScoped
public class MyBean{
myMethod(){
// your method code here
}
}
How ever action listener does the same except that it is triggered with an event
myMethod(Event e){
// your method code here
}
Note that event can be of any type.
onclick works before sending the ajax request i dont have much knowlegde aboput it... i only used it for the UI purposes for example closing a dialog box on clicking a button
<p:commandButton id="cancel" value="Cancel"
icon="ui-icon ui-icon-arrowreturnthick-1-w"
style="float:right;" onclick="PF("dlg").hide()" type="button">
</p:commandButton>
SEE ALSO