3

HI,

I have a doubt on calling the ActionListener method in the JSF beans. For example every request or submission of JSF form is gone through the life cycle of six phases. But, when we are triggering the particular event like action listener or value change listener, is there any lifecycle associated with that request?

Please clarify me.

Krishna
  • 7,154
  • 16
  • 68
  • 80

1 Answers1

10

Any action listener is invoked during invoke action phase, before the real action method. Which action listener methods are to be invoked are determined based on the actionListener attribute of the UICommand component which is associated with the submit.

Any value change listener is invoked during validations phase (or apply request values phase when immediate="true" for the particular UIInput component) after a succesful conversion/validation of the submitted value and only when the submitted value differs from the initial value. Which value change listener methods are to be invoked are determined based on the valueChangeListener attribute of the UIInput components which are associated with the submit.

And no, they do not have their own lifecycle. When they finish executing and return, it's still inside the same phase of the lifecycle. After invoking the valueChangeListener, JSF will continue with conversion/validation of the next UIInput component, or if there are none, then proceed to the next phase. After invoking the actionListener, JSF will continue with the next actionListener or if there are none, invoke the real action method.


Update: after reading your comments again, I think that I now see your doubt about particularly the value change listener. You seem to think that it by default immediately fires a brand new request to the server side during the client side change event. It does that not by default. You can only achieve this by adding a little piece of JavaScript code which submits the entire HTML form during the change event of the HTML input field.

onchange="this.form.submit()"

This part has nothing to do with JSF. It's a simple HTML attribute. Open the page in webbrowser, rightclick and choose View Source. You'll see that it's there. Disable JavaScript in your browser or remove it in JSF code and you'll see that it won't work anymore. You would need to press the submit button yourself to get it all to run.

Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • OK. For example, when we are changing the value in the Combo Box. It will trigger the ValueChangeListener. You mean to say that this will trigger the complete new request to server and page will be reloaded. It will start from the Resore View phase. Am I correct? – Krishna Jan 25 '11 at 04:37
  • 1
    No, it won't. At least, not by default. Only if you call `ExternalContext#redirect()` or `FacesContext#responseComplete()` inside the value change listener method, then it will let JSF jump out of the lifecycle. The first one will abandon and trash the entire request and let the browser fire a brand new GET request on the given URL. The second one will by default result in a blank page. This is often used (in action methods) when you've already written something (a binary file for example) to the response yourself and want to avoid JSF to render the view (and thus corrupt the binary file). – BalusC Jan 25 '11 at 04:41
  • Hi BalusC, I think you have not understood my question. Here I mean to say when changing the values, Will It starts the new life cycle phases starting from the Restore View,etc. In the previous comment you told that, value change listener must be called in the Apply Request Values phase. For that it has start the lifecycle. Am I correct. This question may look simple, i want to get the fundamentals correct. – Krishna Jan 25 '11 at 04:49
  • No. It won't, it stays in the same phase of the same lifecycle (and the value change listener is by default not called in apply request values phase, but in validations phase). – BalusC Jan 25 '11 at 04:53
  • So, when we are changing the value in the combo box, it directly calls the value change method in the Validations phase?. Also will not refresh the entire page? Please correct me if I am wrong. – Krishna Jan 25 '11 at 05:10
  • I already guessed like that. See my answer update which you might have missed. – BalusC Jan 25 '11 at 05:11
  • Thank you. I concluded that Value Change event will not fire the complete life cycle to update the values. But, after validations phase, it will continue with other phases correct till the response? This would be my last question for this conversation :) – Krishna Jan 25 '11 at 05:18
  • 1
    Yes, it's just part of a normal lifecycle. The lifecycle doesn't change based on the way how you fire the HTTP request (by letting JavaScript to submit the form when you change a dropdown/input value, or by pressing the button yourself to submit the form). Either way, the sole HTTP request is the same anyway. – BalusC Jan 25 '11 at 05:26