Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging JSF Life Cycle - what exactly happens in each phase

I have decided to dig completely into JSF 2.0 as my project demands deep knowledge of it. I am reading JSF Lifecyle Debug, a well written and awesome article on JSF Life cycle. While reading this article, I have following confusions.

  1. If it's an initial request, in Restore View Phase an empty View is created and straight Render Response Phase happens. There is no state to save at this point. What actually happens in render response phase then? I am confused a little while I am running the example.

  2. The article states that, retrieved input value is set in inputComponent.setSubmittedValue() in Apply Request Values phase. If validation and conversion passes, then the value gets set in inputComponent.setValue(value) and inputComponent.setSubmittedValue(null) runs. On same point article states that, now if in the next post back request, value is changed, it is compared with the submitted value which would always be null on every post back, value change listener will be invoked. It means if, we don't change the value even, as submittedValue would be null, valueChangeListener will always be invoked? I am confused on this statement. Can someone elaborate on this?

  3. Article states the usage of immediate attribute. If immediate attribute is set on an input component, than ideally Process Validation Phase is skipped, but all of the conversion and validation happens in Apply Request Values. My point is, still when the conversion and validation is happening, what's the advantage of skipping the third phase?

  4. What does the term retrieved value means?

  5. I would like to know, if lets say there are five fields on the view. Does JSF makes a list of some collection of these values and Apply Request Values and Process Validations phase iterate over them one by one?

  6. At the last point of this article where it states, when to use immediate attribute. As per my understanding, if immediate attribute is set in both input component and command component, It will skip the phases from Apply Request Values to Invoke Application for any attribute not having immediate. Then what does the last statement mean "Password forgotten" button in a login form with a required and immediate username field and a required but non-immediate password field.

I know these are very basic confusions but clarity on these topics will definitely help sharpen the JSF knowledge.

like image 506
benz Avatar asked Feb 20 '14 16:02

benz


1 Answers

1: What actually happens in render response phase then?

Generating HTML output for the client, starting with UIViewRoot#encodeAll(). You can see the result by rightclick, View Source in webbrowser (and thus NOT via rightclick, Inspect Element in webbrowser, as that will only show the HTML DOM tree which the webbrowser has built based on the raw HTML source code and all JavaScript events thereafter).


2: it is compared with the submitted value which would always be null on every post back

Nope, it's being hold as an instance variable. JSF doesn't call getSubmittedValue() to compare it.


3: My point is, still when the conversion and validation is happening, what's the advantage of skipping the third phase?

This is answered in the bottom of the article, under Okay, when should I use the immediate attribute?. In a nutshell: prioritizing validation. If components with immediate="true" fail on conversion/validation, then components without immediate="true" won't be converted/validated.


4: What does the term retrieved value means?

The "raw" value which the enduser has submitted (the exact input value which the enduser entered in the input form). This is usually a String. If you're familiar with servlets, then it's easy to understand that it's exactly the value as you obtain by request.getParameter().


5: Does JSF makes a list of some collection of these values and Apply Request Values and Process Validations phase iterate over them one by one?

Almost. The collection is already there in flavor of the JSF component tree. JSF thus basically iterates over a tree structure, starting with FacesContext#getUIViewRoot().


6: Then what does the last statement mean "Password forgotten" button in a login form with a required and immediate username field and a required but non-immediate password field.

This way you can reuse the login form for the "password forgotten" case. If you submit the "login" button, then obviously both the username and password fields must be validated. However if you submit the "password forgotten" button, then the password field shouldn't be validated.


That said, you may find the below JSF phases/lifecycle cheatsheet useful as well for a quick reference:

  • fc = FacesContext
  • vh = ViewHandler
  • in = UIInput
  • rq = HttpServletRequest
  • id = in.getClientId(fc);

1 RESTORE_VIEW

String viewId = rq.getServletPath();
fc.setViewRoot(vh.createView(fc, viewId));

2 APPLY_REQUEST_VALUES

in.setSubmittedValue(rq.getParameter(id));

3 PROCESS_VALIDATIONS

Object value = in.getSubmittedValue();
try {
   value = in.getConvertedValue(fc, value);
   for (Validator v : in.getValidators())
      v.validate(fc, in, value);
   }
   in.setSubmittedValue(null);
   in.setValue(value);
} catch (ConverterException | ValidatorException e) {
   fc.addMessage(id, e.getFacesMessage());
   fc.validationFailed(); // Skips phases 4+5.
   in.setValid(false);
}

4 UPDATE_MODEL_VALUES

bean.setProperty(in.getValue());

5 INVOKE_APPLICATION

bean.submit();

6 RENDER_RESPONSE

vh.renderView(fc, fc.getViewRoot());

See also:

  • Difference between Apply Request Values and Update Model Values
  • JSF - Another question on Lifecycle
  • What's the view build time?
like image 169
BalusC Avatar answered Oct 04 '22 23:10

BalusC