Search code examples
jsfrichfacessession-state

Unnecessary Session Beans in Ajax Enabled JSF Frameworks


I've noticed that when using Ajax heavy JSF 1.2 implementations like Richfaces you're somehow forced to declare more managed beans than you'll want as Session scoped so that state can be maintained across multiple Ajax requests; there are components that will just stop working.

For instance, I developed this application lately in which I had to declare almost all my JSF Backing Beans as Session Scoped in order to have component "x" working. Is there a way out of this, do you consider it a bad practice, or is just the price to pay for having Ajax enabled component in JSF 1.2.

Thanks in advance.


Solution

  • Session scope beans increase memory usage.

    Another available scope is View Scope - This allows to keep a state of a bean between requests, while the user is still on the same view.

    If you are using JSF2, please consider using @ViewScope above the bean name:

        @ViewScope
        public class myBean{
         ..
         }
    

    If you use RichFaces and JSF1.2, consider using <a4j:keepAlive /> under <f:view> in the view. for example:

    <a4j:keepAlive beanName = "#{myBean}"/>
    

    Read more info here