8

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?

Domenic D.
  • 5,276
  • 4
  • 30
  • 41
Lolly
  • 34,250
  • 42
  • 115
  • 150

2 Answers2

19
  • 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 type void, the method either take an ActionEvent 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 type String.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Asif
  • 429
  • 3
  • 10
4

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

Differences between action and actionListener

Community
  • 1
  • 1
Syed Anas
  • 1,459
  • 3
  • 19
  • 38