问题
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?
回答1:
onclickwill be executed first. It is used to call a javascript function.actionListeneris used when you want to have some ajax call to a
method. That method should have the return typevoid, the method either take anActionEventas argument or no argument; it can also be used for a non-ajax call but then the page will be refreshed.actionis used to navigate to a different page; the method should have the return typeString.
回答2:
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
来源:https://stackoverflow.com/questions/21438721/jsf-difference-between-action-actionlistener-onclick