Calling a managed bean method when some button is clicked

onlinenaman picture onlinenaman · Jan 2, 2014 · Viewed 33.7k times · Source

I am new to JSF and want to have a button, which when clicked, will call a method of the backing bean. Is this possible or I have to use AJAX or something? I tried things like

<button onclick="#{myManagedBean.myMethod()}">MyButton</button>

But they didn't work. What's the normal way of doing it?

Edit:
That button is created by JqueryUI, and I am unable to change the button's type to commandButton. I am able to do only two customizations-
1. call javascript functions from that button
2. change the target of the form (inside which the button resides)
So, how can I call the backing bean method from these two ways, like from Javascript or on form submit?

Answer

thiyaga picture thiyaga · Jan 2, 2014

In JSF you have action parameter for the JSF componenents which can be bound to a method in your managed bean. using this method binding you can invoke the method of your managed bean.

<h:commandButton value="click" action="#{managedbean.method}"/>