JSF - Another question on Lifecycle
Asked Answered
R

3

1

Today I'd like to know some features on the JSF Lifecycle. Let me start :

1 - Phase 2:Apply request Values - During this phase,each component in the view will search for its values in the request and set the new values to them

Uhm, ok nice. So, the View will be built due to the previous Beans parameters. After, there is a partial View, generated with the request values. (Right? Later, in the 3° phase, they will be compared) . But, for example, if a values in the request list is absent during the creation of this last view? Values will be null?

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 .

This is not clear at all. At this moment i have (on the beans) the values updated from the previous Phase (If the validation and the apply request aren't failed). Ok, so now what happens? What means the action events queued during the apply request values phase will be processed? It means that, for example, if the action is Submit the process is finished? That's why an ajax call, if not rendered in the 2° phase, will fail? Or where it fails?

3 - Phase 6: Render response - In this phase the component tree will be rendered to the client.

It means that the View on the server is updated by using the updated bean values? And, after this, the HTML code is created from this View? Or just it made the HTML code and save the View status?

Hope you can help me :)

Romanticism answered 1/12, 2010 at 11:12 Comment(0)
D
8

Phase 2:Apply request Values - During this phase,each component in the view will search for its values in the request and set the new values to them

Uhm, ok nice. So, the View will be built due to the previous Beans parameters. After, there is a partial View, generated with the request values. (Right? Later, in the 3° phase, they will be compared) . But, for example, if a values in the request list is absent during the creation of this last view? Values will be null?

Basically the following is happening under the covers (here, input is UIInput and request is HttpServletRequest):

if (input.isRendered()) {
    String value = request.getParameter(input.getClientId());
    if (value != null) {
        input.setSubmittedValue(value);
    }
}

So, they will be untouched if there's no request parameter. They won't be set with null and just kept default.


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 .

This is not clear at all. At this moment i have (on the beans) the values updated from the previous Phase (If the validation and the apply request aren't failed). Ok, so now what happens? What means the action events queued during the apply request values phase will be processed? It means that, for example, if the action is Submit the process is finished? That's why an ajax call, if not rendered in the 2° phase, will fail? Or where it fails?

During 2nd phase basically the following will also happen (here, command is UICommand, request is HttpServletRequest and ActionEvent is ActionEvent):

if (command.isRendered()) {
    String value = request.getParameter(command.getClientId());
    if (value != null) {
        command.queueEvent(new ActionEvent(command)); // Queue for INVOKE_ACTION.
    }
}

Then, during invoke application phase, all events which are queued for the particular phase will be invoked.


Phase 6: Render response - In this phase the component tree will be rendered to the client.

It means that the View on the server is updated by using the updated bean values? And, after this, the HTML code is created from this View? Or just it made the HTML code and save the View status?

During this phase JSF walks through the component tree and all components will be encoded (will invoke the Renderer of all components, by default a HTML renderer). During encoding, the values will just be obtained from the model. The view itself won't be updated. Basically:

facesContext.getViewRoot().encodeAll();
Definition answered 1/12, 2010 at 14:10 Comment(11)
Second Point : still I don't understand (Phase 5). What mean "do the action"? Basically i've just done it, by getting the values from the request and put in the UI Context (if, for example, the action is SubmitRomanticism
Execute the bean method which is declared in action attribute of the UICommand component -if any. E.g. <h:commandButton action="#{bean.submit}" /> will invoke submit() method of bean during invoke action phase. See also this article.Definition
Ahh...ok clear :) And, as because the model is updated in the 4° phase, it use the updated values, right?Romanticism
If the action method is in the same model or has access to the desired model (managed property and so on), then obviously yes!Definition
Fine. Thanks. Last Point You said During this phase JSF walks through the component tree and all components will be encoded. That mean that start at the top of the component tree and get the current value from the updated model, right?Romanticism
Whenever applicable, yes. If any of the component attributes contains a value expression #{bean.something}, then it will be evaluated and its result will be included in the rendered output.Definition
Nice one. Now is more clear! These things could be banal for many. But i think its necessary to understand them very well! Thanks again master :)Romanticism
You're welcome. If you understand basic Servlet API (which is IMHO mandatory before you dive into JSF), then you may find this answer useful as well to understand how the average MVC framework works on top of Servlet API. This way you'll better understand what the heck is happening under the covers of such a MVC framework ;)Definition
I done a project time ago (for the university) by using servlet+jsp. I understand that logic. But this, in my opinion, is TOTALLY DIFFERENT. JSF Lifecycle is more complex!Romanticism
Yes, you have to understand it in general, but you end up with simpler code. Just a XHTML file as view and a javabean as model. No hassle with manually request parameter gathering, converting, validation, action allocation and invocation, etc.Definition
@Definition if you write a book , I'll be the first to buy it.Aubyn
L
2

1 - Phase 2:Apply request Values

This phase has nothing to do with the backing bean. Here, we just process the request parameters, and try to "link them" to a component in the view. During the Apply Request phase, these HTTP request parameters need to be converted and validated before they are injected into the actual bean.

2 - Phase 5: Invoke Application

In JSF, ActionEvents are handled by ActionListeners. When clicking the button, an ActionEvent is raised and queued to be processed at a later phase. A default Actionlistener is provided by JSF that picks up this ActionEvent and processes it in the Process Application Phase.

The Default ActionListener is implemented in such a way that it evaluates the EL expression, and uses the outcome to pass it on to a navigationlistener. So what you take for granted (pressing a button executes the EL expression in the action attribute and forwards to another page) is actually handled internally using an ActionListener.

3 - Phase 6: Render response -

A JSF view is an internal representation of the component tree and all value bindings. The actual HTML representation is handled by the ViewHandler.

Lambart answered 1/12, 2010 at 14:10 Comment(0)
L
-1

Another excelent article about JSF lifecycle can be found at: http://palkonyves.blogspot.com.br/2013/08/demistifying-jsf-21-lifecycle-mojarra.html

Lissome answered 27/1, 2015 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.