3

I was reading an article today about Lifecycle on JSF.

I have some trouble to understand these points :

1 - Phase 3 :Process validations - This is the phase in which the component may validate their new values. If the new value is valid and differs form the previous value a value-change event will be created and put in a queue. So in our example if the user change the name before submitting the form, a ValueChangeEvent object will be created by the UIInput component Object corresponding to the Name textbox and queued it for processing at the end of this phase.This is how the valueChangeInput method in the backing bean get invoked.

How can JSF know the difference between old value and the new one? The instances of the View Object are 2? The previous (the one before the request) and the new? (which have the values on the FacesContext added by the last process, Apply request Values)

2 - Phase 5: Invoke Application - Once all the values of the request has been successfully set to the backing bean the action events queued during the apply request values phase will be processed. In our case the submit buttons action method .

So it send directly the instance of FacesContext to the last phase (Render response) that convert UI elements (and their values) to Html. So, when is that the getter methods (of the bean) are called?

Cheers

markzzz
  • 47,390
  • 120
  • 299
  • 507

1 Answers1

5

How can JSF know the difference between old value and the new one? The instances of the View Object are 2? The previous (the one before the request) and the new? (which have the values on the FacesContext added by the last process, Apply request Values)

The old value is the current model value. The new value is the submitted value. With following example,

<h:inputText value="#{bean.value}" />

JSF will basically do the following (conversion/validation omitted for brevity):

Object oldValue = bean.getValue();
Object newValue = request.getParameter(clientId);
if (oldValue == null ? newValue != null : !oldValue.equals(newValue)) {
    // Create and queue ValueChangeEvent.
}

So it send directly the instance of FacesContext to the last phase (Render response) that convert UI elements (and their values) to Html.

Not exactly that way, but yes, the render response will kick in when the invoke action is finished.


So, when is that the getter methods (of the bean) are called?

Yes, that happens during render response only when they are bound in the view.

You can find here another article which explains the JSF lifecycle in a more practical manner.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • About answer at first point : Ok, it get the value from the bean. But if the bean is request scoped all value will be null when i create the view. (and the valueChange handler should be called?). About second point : i don't understand. So it set the values in the View Instance (FacesContext) at the first point of lifecycle, and if validation (or conversion) dont make error it take the same value from the bean? Sound bizzarre, that's i said i don't have understand :) I looked at your article this afternoon... but still not clear. I need to practice again... – markzzz Nov 27 '10 at 20:26
  • The value will not be null if it's preset in constructor or postconstruct of the bean. The value change listener will be invoked when the submitted value differs from the initial model value. If there are no errors, it will display the model values (which are set from submitted values during update model values phase!). If there are errors, it will display the submitted values (because model values cannot be set due to the error). – BalusC Nov 27 '10 at 20:35
  • Step by step (else i make more confusion). FacesContext is the object with values getted by "the old model" (saved into the Bean), right? (Created at the first point of lifecycle. I talk about postback request...) – markzzz Nov 27 '10 at 20:48
  • No, the `FacesContext` is kind of a JSF lifecycle manager. It knows about everything. It knows which request parameters are there. It knows which managed beans are there. It knows which validators/converters are there. It knows how the component tree is structured. It knows what phase of lifecycle it is in. It takes care of executing the JSF lifecycle all the way from begin to end based on the information it knows about. It doesn't "contain" any values. The model values are just in the managed beans. Do you know about the basic JSP/Servlet API? – BalusC Nov 27 '10 at 20:56
  • JSF runs on top of the Servlet API and abstracts it all away to a high degree so that you end up with a XHTML file as View and a managed bean as Model (the `FacesServlet` is the Controller). Anyway, you may find [this answer](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/3542297#3542297) useful as well to learn how JSF roughly works "under the covers". – BalusC Nov 27 '10 at 21:01
  • Uhm. So i misunderstand that guide. In the 2° point of lifecicle, it say "Please note that in this phase the new values are set only to the component objects only, not to the backing bean instance variables." As you said, it doesnt set any values on the object... – markzzz Nov 27 '10 at 21:05
  • Indeed, it's set in the component tree. It's the one as returned by `FacesContext#getViewRoot()`. It contains the server side state of all the components you have definied in the view (the XHTML file). See also the detailed explanations in my article. – BalusC Nov 27 '10 at 21:08
  • OK. I understand the logic of bean (setter and getter) on the model side. Question remain on bean scope : if the bean is requested scoped, after Render response it will destroy. So, at the next postback, in the second phase (Apply request Values) i couldn't apply to the component tree the old model values (because should be the values to the previous bean, that is desroyed). Then i think phase 2 and 3 will be ignored? – markzzz Nov 27 '10 at 21:17
  • 1
    Those phases will not be skipped. The bean will just be recreated. In case of a request scoped bean, whatever values are been set during bean's (post)construction will be used as "old" values. – BalusC Nov 27 '10 at 21:20
  • ah ok! Thanks for the delucidation, as ever! You are my personal jsf-rescue these days :) Now ive just to understand why my code doesnt work hehe – markzzz Nov 27 '10 at 21:24
  • You're welcome. If you're struggling too long with a problem with your current code, just press `Ask Question` here. – BalusC Nov 27 '10 at 21:28
  • To be honest i already post my problems http://stackoverflow.com/questions/4292330/jsf-ajax-call-what-is-it-wrong-on-this-code :) – markzzz Nov 27 '10 at 21:30
  • Ah, I must have missed this one. – BalusC Nov 27 '10 at 21:33
  • Ill wait some hours before accept this question. If some trouble come about the lifecycle, i'll up this comments. Don't worry, you now i always put accepted answer :) – markzzz Nov 27 '10 at 23:10
  • You could just ask a new question. The concrete question is already been answered. – BalusC Nov 27 '10 at 23:36