Tuesday, May 25, 2010

solving richfaces component null data with rich:keepAlive

Using RichFaces components, we encounted null data when we submitted a datatable with h:inputText or rich:inplaceSelect or rich:inplaceInput.


The solution: using rich:keepAlive.


In this post, I will try to explain it.


When coming to develop a quick and well-built webapplication, I decided to use only request scope beans. (Well, my application didn't need any cart object ).

JSF beans have 4 scopes:

none, session, request and application. (I found the followings here).

 None: Objects with this scope are not visible in any JSF page. When used in the configuration file, they indicate managed beans that are used by other managed beans in the application. Objects with none scope can use other objects with none scope.
• Request: Objects with this scope are visible from the start of the request until the end of the request. Request scope starts at the beginning of a request and ends when the response has been sent to the client. If the request is forwarded, the objects are visible in the forwarded page, because that page is still part of the same request/response cycle. Objects with request scope can use other objects with none, request, session, or application scope.• Session: An object with session scope is visible for any request/response cycle that belongs to a session. Objects with this scope have their state persisted between requests and last until the object or the session is invalidated. Objects with session scope can use other objects with none, session, or application scope.• Application: An object with application scope is visible in all request/response cycles for all clients using the application, for as long as the application is active. Objects with application scope can use other objects with none or application scope.

Why not to use session scope beans?


  1. Using the session scope significally slows down performance.The bean will unnecessary reside in the memory even after your work is done. When using the session or application scope bean you have to explicitly remove it after you are done. Otherwise the session bean will be there in the memory till you explicitly remove the bean or your server removes it after a configured time limit.. It reduces the performance by imposing overhead on memory and garbage collector. 
  2. If the user changes datatable data in a session scope bean, and i haven't removed the data from that bean, and in my method i ask if (thelist!=null) when he returns to the screen, the data will be the same.



If JSF came in to seperate view and controller, using sessionMap.remove("userBean");
could confuse users for what bean is still in scope and what is not.

However, using RichFaces components requires the bean to stay alive between the request.

Here comes rich:keepAlive tag. it keeps the bean exactly for the viewed page, and is in charge of removing it from the session.
Put your rich:keepAlive after the element.

Voila!

No comments:

Post a Comment