Friday, 20 March 2015

Understanding Immediate Attribute

The immediate attribute is highly misunderstood by thousands of developers. The misconception with immediate attribute is that if we set it as true – “It skips validation”. Well, it is not always true. Let us see how!!

Before reading you should know how JSF life cycle works. For reference you can check out my previous post in this link.


Now in general, the immediate attribute allows processing of components to move up to the “Apply Request Value” phase of JSF life cycle.  But the behaviour of immediate attribute depends on which adf component it has been applied on.



Where Can I use Immediate = true for commandButton ?

Sometimes we might have a form for inserting values for a record. We must need a “Cancel” button there to undo the changes and navigate to some previous page. What we normally do:

·         We clear the iterator from the garbage values. And then navigate our page
We can achieve the same functionality using immediate = true:
·         We can set immediate property of the cancel button as true. When user clicks on Cancel button, all validations are skipped, any entered data is not updated to the model and the user navigates to previous page.

How is this more efficient?

·         It avoids validations and update model phase thus saving time.
·         The iterator doesn’t need to be accessed which again saves memory and time.

Where Can I use Immediate = true for components like inputText?

Setting immediate to true for an input component can be useful when one or more input components must be validated before other components. Then, if one of those components is found to have invalid data, validation is skipped for the other input components in the same page, thereby reducing the number of error messages shown for the page.

What if I have used an inputText with immediate = false but commandButton with immediate = true?

This situation may lead to a problem in the following scenario:
·         The commandButton is calling a method in managed bean. You need to access the data entered by the user in the inputText in this bean method.
·         Now the problem here is, as the immediate property is set as true for command button; the actionListener has been called in the “Apply request Value” phase only. So the data has not been updated to the model. So we won’t be able to get the data from the iterator

How do I get the value in the managed bean then?

In order to get the value in managed bean, you need to set the immediate property of the inputText as true as well.
·         This way the valueChangeListener event for the inputText will be done in the “Apply Request Value” phase itself. Then using the bindings of the inputText we can have the data entered by the user.

Understanding JSF Life Cycle

The JSF lifecycle phases use a UI component tree to manage the display of the faces components. This tree is a runtime representation of a JSF page: each UI component tag in a page corresponds to a UI component instance in the tree. The FacesServlet servlet manages the request processing lifecycle in JSF applications. FacesServlet creates an object called FacesContext, which contains the information necessary for request processing, and invokes an object that executes the lifecycle.
The JSF Life Cycle is represented by the following diagram:



Photo Courtesy: Web User Interface Developer's Guide for Oracle ADF
Let’s understand each phase of the JSF lifecycle:
·         Restore View: The component tree mentioned earlier is established. If this is not the initial rendering, the tree is restored with the appropriate state. If this is the initial rendering, the component tree is created and the lifecycle jumps to the Render Response phase.
·         Apply Request Values: Each component in the tree extracts new values from the request parameters and stores the values locally. If a component has its immediate attribute set to true, then the validation, the conversion, and the events associated with the component are processed during this phase.
·         Process Validations: It is one of the most important phases of JSF life cycle. The process validation is done through the following steps:
§  Conversion: The local values of components are converted from the input type to the underlying data type.
§  Required Check: If there are no failures in conversion, the required attribute on the component is checked. If the value is true then it goes for validation.
§  Validation: If the required check condition is true, and the associated field contains a value, then any associated validators are run. If the value is true and there is no field value, this phase completes (all remaining validators are executed), but the lifecycle jumps to the Render Response phase.


Condition checking in Validation Process Phase
·         Update Model Values: The component’s validated local values are moved to the model, and the local copies are discarded.
·         Invoke Application: Application-level logic (such as event handlers) is executed
·         Render Response: The components in the tree are rendered. State information is saved for subsequent requests and for the Restore View phase.