The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Wednesday, June 4, 2008

Scope options in ADF

ADF supports all the usual scope options as in JSF like none, requestScope, session scope and application scope. Along with these scoping options ADF supports one additional highly useful scope option that is, pageFlowScope. Let's talk about these options one by one in brief:

Let's take an usecase to understand it better. Say, there is a a page 'pageA' in your application having a selectOneChoice and a submit button. On clicking the submit button it navigates you to 'pageB'. There is a bean method assoicated with the selectOneChoiceList's value change listener. this method is simply storing the selected value of the list to a bean property.

1. none: So, with none scope, on changing the the value in the list, the associated bean will be instantiated and the associaed listner method will be called. As soon as the control returns from the bean method the bean will be destroyed. In other words, you cannot access the bean property value after that even on the same page.

2. requestScope: if the bean scope is requestScope then the value stored in the bean property will be accessible even on the pageB. you can simply access it at #{requestScope.yourBean.yourPropertyName}. This bean will be destroyed as soon as you submit another request by say clicking back button to navigate to pageA or by clicking a submit button to do some other action which generates another request to the server.

3. pageFlowScope: This scope is an introduction of ADF. In ADF you can group some pages (called activity) in a logical unit called TaskFlow (more specifically a bounded task flow in this case). Say you have two more pages pageC and pageD and you can navigate from pageA to pageB to pageC to pageD using some next button on each page. Now, suppose you have created a bounded task flow using pageA, pageB and pageC. So, if your bean is of pageFlowScope then after selecting a value in list at pageA if you navigate till pageC, you can still access the bean's property. It persists within the taskflow boundries. But, if you navigate to pageD then the bean will be destriyed. pageFlowScope is kinda more broader than requestScope and smaller than a session scope. For most of the scenarios in ADF you create a bounded task flow to implement a business use case. So, in way pageFlowScope makes more sense than a session scope. Ofcourse, session will be required in cases where you need to persis the values out of a taskflow boundry. You can access it using an EL expression like #{pageFlowScope.YourBean.yourPropertyName}.

4. sessionScope: Continuing with the same example, the bean once instantiated will persist till the entire user session. This session usually persists till a user is logged in. Usually, web servers are configured for a session time out property which makes the session invalid after the specified period of time. So, the bean's property can be access anywhere for that session. Usually, in real life scenarios information related to user like its profile are set in to the sessions.

5. applicationScope: As the name suggests this scope persists as long as the application is running. Ususally, application scope parameters are initialized at the time of application start-up (web server start). Generally, some all time needed resources are initialized using applicationScope which are required all the time and are independent of any specific role or responsibility.


Frank Nimphius said...


there is one additional scope, which is the viewScope. The ViewScope lasts as long as the ViewId doesn't change. This is longer than request scope and shorter than pageFlowScope

Anonymous said...

thank you so much, you saved a ton of my time!

Anonymous said...

hi, there is one additional scope: backing bean

log said...

Hi Vivek,

It is really nice posted... I am running with one major problem please help me.. In my ADF application, I have created the java class with I am using as a backing beam for my page fragments... But I could not able to set that bean scope other than request...so I am losing the values when I moved out of page A to Page B.. my pageFlowScope losing the value... IF I set the managed-bean-scope.. other than request getting target unreachanble error... Please help me how to solve this..


Anonymous said...

hi vivek,
i have one query related to session scope in ADF pages. Does ADF creates new Session object for every new page? If so, then how to maintain same session for particular user in ADF?
Problem is, in my application, iam storing attributes in the session in login page but, as soon as next page loads, that session is geting destroyed and new session is being created and hence my old attribute data is null in the new page.
Can you tell how the user session can be persisted till he logs out??

I am getting http session for any request by passing false every time like:
getSession(false), to avaoid creation of session.

Unknown said...

Quite strange to hear... should not be the case....

Usual practice is to create a managed bean stored in session scope. and it should work until u explicitly destroy session or browser closed or crashes.