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.

2 comments:

  1. Good Understanding on Immediate Attribute on various Components.

    ReplyDelete
  2. It's really helpful...Thanks again Pankaj.

    ReplyDelete