PageFlow: how to use a differents session in each tab of one navigator

| December 9, 2011 | 0 Comments

trinidad_logoWe have developed a JSF application using Trinidad as implementation, which uses the session bean to maintain user state logged in the application and its settings. If we open a new tab in the same web browser or another instance of the browser and access the application will be accessing the same session.

¿What is the problem?

Assume that the application uses a series of data per year, so that the year through which the individual user is browsing is stored in a session bean. If the user wants to navigate through another year at a time the only option is to access the application using another browser other.

And the solution is…

The solution is related to the different scopes available in Apache Trinidad JSF. We can use any of the three scopes JSF by default:

- applicationScope

- sessionScope

-requestScope

But none of them solves our problem, because we want a scope to keep the information between pages, but is different in each of open browser tabs (still the same session for both tabs).

Dealing with this problem we can choose between two possible solutions:

  1. Change the scope of the bean that stores the data for the year to request and modify all requests made for this bean to add as an input parameter the year in question you want to access.
  2. Using a scope present in Apache Trinidad JSF PageFlow called.

The first solution is highly intrusive so it does not recommend its use, we will discuss the second solution.

Solution Analysis

The first aproach to the solution would be to replace the bean configuration file for the scope PageFlow. But official documentation say us it is not possible to  use <managed-bean-scope> with scope pageFlow.

Suppose the bean that is stored in the session is as follow:

1
2
3
4
5
6
7
8
9
10
11
package es.mergetag.pageFlow;
public class ConfiguracionSesion {
     private Year year = null;
     public Year getYear()
    {
      return year;
    }
     public void setYear(Year year) {
        this.year = year;
    }
}

and inside this object there is an Year object representing a year throught the user want to navigate at this moment. So the year is stored also in the session along with the object of session configuration.

1
2
3
4
5
package es.mergetag.pageFlow;
public class Year {
        //content of data year
 
}

So, at some point in the application may need a onfguration file that make uses of this bean:

<managed-bean>

<managed-bean-name>configuracionBean</managed-bean-name>

<managed-bean-class>es.mergetag.pageFlow.ConfiguracionSesion</managed-bean-class>

<managed-bean-scope>session</managed-bean-scope>

<managed-property>

<property-name>year</property-name>

<value>#{year}</value>

</managed-property>

</managed-bean>

Having the configuracionBean stored in session, the year also is in the session. If we want to change the scope we can to modify putting: <managed-bean-scope>pageFlow</managed-bean-scope>. However, this is a limitation of the scope that indicates in the official docs of Apache JSF Trinidad.

Solution development

We need to make if from any other part of the application variable is accessed variable years into the configuration bean with session scope, get the PageFlow year. A completely transparent solution would be to change the get and set methods in the bean configuration:

1
2
3
4
5
6
7
8
9
10
11
public void setYear(Year year){
 
this.setYear(year);
 
}
 
public Year getYear(){
 
return this.year;
 
}

 

to:

 

1
2
3
4
5
6
7
8
9
10
11
public void setYear(Year year){
 
RequestContext requestContext = RequestContext.getCurrentInstance();   requestContext.getPageFlowScope().put("year", year);
 
}
 
public Year getYear(){
 
RequestContext requestContext = RequestContext.getCurrentInstance();     this.year = requestContext.getPageFlowScope().get("year");
 
}

Conclusion.

By this way, we have only modifed the JavaBean PageFlow from we  wanted to take the field while keeping the rest of the application intact and thus getting a user to manage different versions of the same bean in different tabs of the same browser.

Note that indicated in the official documentation is in a state PageFlow experimental.

____________________________________________________________

Links references:

Web Apache JSF Trinidad: http://myfaces.apache.org/trinidad/

Official docs about pageFlow:  http://myfaces.apache.org/trinidad/devguide/communicatingBetweenPages.html

 

 

 

 

 

 

 

Tags: ,

Category: Development, Java

Leave a Reply