280 CHAPTER 6: Component-Based Web Development Using JSF 2
- Now the JSF runtime enters the second phase: Apply Request Values. The
UIForm object corresponds to the h:form, the UIInput object corresponds to
the h:inputText, and the UICommand object corresponds to h:commandButton
in the JSF file. Since this is the first request to this page and there are no
request parameters available or events to process, nothing to update the
model with, nothing to convert and validate, and no application-level events,
the JSF runtime skips the second, third, fourth, and fifth phases and enters
the sixth phase: Render Response. Each component object in the created
component tree has a renderer that generates HTML. This process of
generating HTML by the renderer of the component is called encoding. This
encoded page is then displayed in the browser. - The user now fills in the name field in the form and clicks the Submit button.
- The browser sends the form data to the web server, formatted as a POST
request. The JSF runtime enters the first phase again, Restore View, and the
component tree is restored to reflect the values the user entered in the form.
And then the JSF runtime enters the second phase, Apply Request Values. - In the Apply Request Values phase, the JSF runtime performs the process
called decoding in which each component in the component tree decodes
the form data and the component stores this value. If the conversion fails
while storing, an error message is generated and queued on FacesContext. - The JSF runtime enters the third phase: Process Validation. At this point, the
JSF runtime processes the validators registered with the component tree
during the first phase. If there are any validation errors, such as if the name
field in the Hello World application is empty, JSF adds an error message
to the FacesContext instance, skips the other phases, and enters the sixth
phase: Render Response. It also displays the error messages, such as
“name field cannot be empty.” Since there is no validation in the Hello World
application, the JSF runtime enters the fourth phase: Update Model. - In the Update Model phase, the JSF runtime updates the managed bean,
namely, helloBean’s property name, with the value entered on the form.
The UIInput component updates the helloBean property name referenced
in the value attribute and invokes the setter methods with the value that
the user entered. In this phase, conversion is also performed if necessary
via the converters registered with the component. Since the conversion is
not necessary in this case, the JSF runtime enters the fifth phase, Invoke
Application, or triggers an action event.