When it comes to variables, we need to use them carefully as wrong usage may lead to unnecessary memory consumption and confusion if the associated memory is not released on time. Example, once an object with application scope is assigned a value, it retains the value for the entire application irrespective of user session.
So, lets see how objects with different scopes behave and used.
Different Scopes:
- Application Scope: Used to store information which are session independent. Static information which are the same for all users and are available for the entire application.
- Session Scope: Information like user related data which need to be available from the session start to session end are stored in the session scope.
- PageFlow Scope: Available for the duration of a task flow and exit while exiting the task flow.
- View Scope: Stores object for the current view and releases while navigating to a different view or there is a change in view id.
- Request Scope: Stored object is available for the current HTTP request and exits when a response is sent back to the client.
We will go through an example where we can see how objects with different scopes work.
Hands-on:
Let’s start with creating two pages on an unbounded taskflow and a bounded taskflow with two more pages.
The first jspx is created with an input text and a button which sets the input value to different scopes. It has also another button to navigate to another jspx in the same unbounded task flow.
From the second page, we will invoke a bounded taskflow. The bounded taskflow has two pages. All the pages have the same items which display the information stored in different memory scopes.
Lets apply a user-input in the text input field of testScopePG and hit ‘SetApplicationScope’. Fields with all the scopes(Including View and Request) will be populated with the same value.
The values for the input items are:
#{sessionScope.KEY}
#{viewScope.KEY}
#{applicationScope.KEY}
#{pageFlowScope.KEY}
#{requestScope.KEY}
#{viewScope.KEY}
#{applicationScope.KEY}
#{pageFlowScope.KEY}
#{requestScope.KEY}
Now, click ‘NavigatetoPG1’ which will navigate to testScopePG1. Here we could see that Viw and Request scope fields are empty.
View attribute is null as it retains as long as the view(viewid for page) doesn’t change.Request attribute is null as the NavigatetoPG1 event resulted in a new http request.
Lets call the taskflow by clicking ‘TF Call’. We see that the PageFlow attribute became null. Its because there is a change in workflow as we navigated from the inbounded taskflow to the bounded taskflow.
Throughout the testing so far, we see that Application and Session scopes are retained.
Now, lets close our browser and click on the ‘Target URL’ link for testScopePG on the log as shown below and see that the session is now gone and its just the application scope that is retained.
Source Code for the managed bean:
In the above example, the below code is written to set the attributes at different scopes.
Below is the code for ‘SetApplicationScope_action()’ associated with ‘SetApplicationScope’ button.
ليست هناك تعليقات:
إرسال تعليق