PhotoAlbum Application Download Support Developer Guide
Resources/Beans Handling
Ajax Miscellaneous
Tag Information

a4j:keepAlive extends the live cycle for request scope beans. In general, each Ajax request works inside the own request scope. Therefore, the state of the component tree rendered based on the variable from one request scope does not correspond to the state of the same tree processed during the next Ajax request. This causes one of the most widespread problem when the form elements (buttons or fields) can't be correctly proceeded with Ajax if the 'rendered' attribute is mapped to the request scoped variable

Using the session scope variables solves the above mentioned problem. However, using a session scope might be inappropriate from the architectural point of view. a4j:keepAlive allows to reuse the same value for a request scope bean between several Ajax requests.

beanName attribute references to a request scope bean name you'd like to re-use during the Ajax requests.

ajaxOnly attribute is a flag that declares whether the value of the bean should be available during a non-Ajax request. If ajaxOnly="true", a request scope bean will keep the same value during Ajax requests from the given page, but it will be re-created as a regular request scope bean when a non-Ajax request will send from this page.

KeepAlive demo

This example will not work as expected. The expression for 'disabled' attribute equals false only after UPDATE model phase when the rsBean properties are updated with submitted values. So, the component is rendered as enabled on the RENDER RESPONSE phase.
However, those values do not make sence during the next Ajax request. The rsBean is created from scratch as soon as it is a new request. JSF makes a decision what is process on the second (APPLY VALUES) phase. At this moment, the expression for 'disabled' still equals true. Therefore, the processing for button is bypassed. The action does not invoked as a result. phase

This example will work properly. The code is almost the same as for example on the left. Used managed been is based on the same class. The only difference is a4j:keepAlive beanName="rsBean2". This statment declares to keep the value of the rsBean2 between the Ajax requests.
The rsBean2 is still a request scope bean as it is defined in the faces-config.xml file. a4j:keepAlive stores the rsBean at the end of the JSF lifecycle. At the beginning of the next cycle, it updates with the rsBean2 with the stored data. Therefore, the expression for 'disabled' equals true on the second phase. The button is processed and the action is invoked.


View Source
RichFaces Wiki RichFaces Project Site