Monday, January 25, 2010

Conversation Scope Jspx Bean



Jspx bean is a very powerful feature in Jspx. Most of the time in web development; developer is required to link between Model and View. Sometimes this link is read only (Displaying Model extracted from Data Layer) or Read/Write (Collecting Data from the user in the View Layer and pass it to the Data Layer for persistence).
More Info about the Jspx Bean is found here
A very famous Business case is Form Entry like customer registration; where user fills in a form to compose a model. In such case the size of the Model properties is a factor in complexity of the development.
Jspx provides Jspx Bean to create the Read/Write Link between View and Model. In many different cases it is required to maintain the Model Data for later use. In some cases like a wizard, where the user is prompted through different steps; data entered should be maintained till the end of the wizard.
Initially Jspx provided to Maintainability Scopes (Session, Request).

The Request Scope is the default Scope for a bean. In Request Scope, the Bean is only life during one post-back cycle. So when the page is rendered the bean is destroyed.
The Session Scope is used to keep the bean live for a longer time. A bean has a Session Scope is visible by all other controllers in the project. Controller can share the same Session Scope Jspx Bean already declared by declaring a Session Bean with the same name.

However, a problem may occur due to the conflict of the name. Also, in case of wizards, the beans should be declared as Session Beans. This violates the concept of multi threading in a web application. As if the end user opens different windows on the wizard, he will be ending overriding the same bean.

For such a problem, the Conversation Scope Bean is used. The concept of Conversation is introduced as a solution for the problem of sharing beans in Session Scope and the stateless problem of the Request Scope.
Conversation scope beans are only visible to the pages that are declaring this bean. Other instant of the page or the wizard will create new instant of the bean with fresh values.
What about moving from one page to another? In a wizard example, the Developer should advance the user to the next page using dispatching dispatch(String newPage), rather than the redirecting. The target page should also declare a Conversation Scope Jspx Bean with the same name. By this way, the framework will populate the bean with the value of the bean in the previous page.
Still there is a drawback for such feature that is the fact that Conversation Beans are stored in the User HTTP Session. So for N number of times the user opens a page declaring a Conversation Bean, a new instant is created and stored in the session.
Cleaning up the Conversation Scope beans is not automatically done. So the developer should call the clearJspxBean(String jspxBeanName)    at the end of the conversation in order to remove the Bean from the Session.


No comments:

Post a Comment