JSF difference between action, actionlistener, onClick

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

+8
jsf jsf-2 primefaces
source share
2 answers
  • onclick will be executed first. It is used to call the javascript function.

  • actionListener used when you want to get some ajax call for a method. This method must have a return type of void , the method either take an ActionEvent as an argument, or there is no argument; it can also be used to call without an ajax, but then the page will be refreshed.

  • action used to go to another page; The method must have a return type of String .

+15
source share

This question has been asked before. The action is used when you want to call a method in your bean support. eg

 action="#{myBean.myMethod}" 

The code for bean will be similar to

 @ManagedBean(name = "myBean", eager = true) @ViewScoped public class MyBean{ myMethod(){ // your method code here } } 

As always, the action listener does the same, except that it fires with an event

 myMethod(Event e){ // your method code here } 

Please note that the event can be of any type.

onclick works before sending an ajax request. I don’t know much. I just used it for user interface purposes, for example, closing the dialog when I click

 <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

+2
source share

All Articles