IMMEDIATE ATTRIBUTE OF JSF COMPONENT
In this blog we will see the impact of the “immediate”
attributes in the life cycle of a JSF page.
And also discuss the scenarios where the “immediate”
attributes can be used.
“immediate” is one of the property of JSF component. It
holds Boolean value. By default it is 'false'. But when you set the immediate
="true" to any component its conversation,validation and updation of
model happens in the second phase of the JSF life cycle(apply request phase).
For all those component who are not set immediate="true" follow the
same life cycle process.
If the component is action component then “invoke application phase” operation is
performed in the 'apply request phase'.
Before we start let see the
life cycle phases of any JSF page.
The JSF page life cycle has six phase:-
1. Restore View Phase
2. Apply request Phase
3. Process validation Phase
4. Update model phase
5. Invoke application phase
6. Render
response phase
Here is the example for immediate attributes :-
Backing Bean :-
Html page :-
Flow without “immediate=true” :-
1. Restore
View :- The View is restored from the session.
2. Apply
Request :- The submitted form values are store in the relevant
component in the UIViewRoot. In this case the “name” enter by the user is store
in the name component.The value is then converted into the data type of the
property. In this case “name” is of String type, so the it is mapped into
String. If name was Integer then value is converted into the Integer type.
3. Process
Validation :- The value store in the component is validated.
4. Update
Model :-
After confirming that data is valid in the previous phase local values
of components can be set to corresponding server side object properties i.e.
backing beans. So bean properties will be updated .In this case the “name”
property of the “ImmediateSampleBean” is updated.
5. Invoke
Application :-The components values are converted , validated and updated. In
this phase the business logic is executed. In this case the “action” method of
the “ImmediateSampleBean” backing bean is executed.
6. Render
Response :-In this phase JSP container renders the page back to the user i.e. view is displayed with all of its
components in their current state.
Flow with “immediate=true” set for input :-
If set the immediate=”true” to the inputText for the name
then. Validation and Converstion of the submitted value for the “name” input
will happen in the second phase of the page Life cycle.
1. Restore
View :- The View is restored from the session.
2. Apply
Request :- The submitted form values are store in the relevant
component in the UIViewRoot. In this case the “name” enter by the user is store
in the name component. The Conversation
and Validation of the submitted value happens in this phase. If any error
occurs then the message is displayed and controls goes to the Render Response
Phase.
3. Process
Validation :- Nothing happens here.
4. Update
Model :- Backing bean properties will be
updated .
5. Invoke
Application :- The components values are converted , validated and updated. In
this phase the business logic is executed.
6. Render
Response :- The Page is render back to the user with updated values.
Note :- many
people believe that “immediate” is used to skip the validation. But validation
happens. The difference is that validation happens in the earlier phase.
For example :-
If we do not pass any value to the input component then the error message “Name
is required” is displayed.
For above code if user don't provide any value and click the "action" button then following error message is displayed in the screen.
Flow with “immediate=true” set for CommandButton :-
If set the immediate=”true” to the “commandButton” then the
action is performed in the “Apply Request Phase” rather than waiting for the
“Invoke Application Phase”.
1. Restore
View :- The View is restored from the session.
2. Apply
Request :- The submitted form values are
store in the relevant component in the UIViewRoot. In this case the “name”
enter by the user is store in the name component. The real
processing of the form submission happens here. This happens in this phase
instead of the Invoke application phase due to the immediate="true"
in the h:commandButton. The UIInput components which don't have
immediate="true" set will not be converted, validated nor updated
3. Process
Validation :-This phase is skipped due to the immediate="true" in the
h:commandButton.
4. Update
Model :- This phase is skipped due to the immediate="true" in the
h:commandButton.
5. Invoke
Application :- This phase is skipped due to the immediate="true" in
the h:commandButton.
6. Render
Response :- The Page is render back to
the user.
Note :- As the
“Update Model Phase” is skipped the submitted values are not updated. So
nothing will be displayed in the outputText.
Flow with “immediate=true” set for both InputText and CommandButton :-
If set the immediate=”true” to the “commandButton” then the
action is performed in the “Apply Request Phase” rather than waiting for the
“Invoke Application Phase”. Also for the
properties which are set
immediate=”true”, the conversation , validation and updation of the model
values happens in this phase only.
1. Restore
View :-
The View is restored from the session.
2. Apply
Request :- The submitted form values are store in the relevant
component in the UIViewRoot. In this case the “name” enter by the user is store
in the name component.The conversation , validation and updation of model values
happens in this phase for components which are set immediate=”true”. In this
case “name”. Then the
business logic is executed that is action method is called.
3. Process
Validation :- This phase is skipped due to the immediate="true" in
the h:commandButton.
4. Update
Model :- This phase is skipped due to the immediate="true" in the
h:commandButton.
5. Invoke
Application :- This phase is skipped due to the immediate="true" in the
h:commandButton.
6. Render
Response:- The Page is render back to the user with updated values.