tags:

views:

177

answers:

2

Hi , I am using . and my managed bean is defined in session scope. when I open a new browser , the constructor of the managed bean is getting called for the first time. but after that, when I open another browser instance, I see directly the method is getting called instead of going thru the constructor of the managed bean.

Is there a specific way to do, to create an new instance of managed bean for every browser instance in jsf

Apprecitate any help

Bob

+2  A: 

The "problem" is in your webbrowser. It is sharing the same session among all instances/windows/tabs. This is normal behaviour. In JSF 2.x, you could "fix" this by placing the bean in view scope, which would keep the bean alive as long as you're submitting and navigating back to the same view everytime. In JSF 1.x, which lacks the view scope, you could achieve the desired behaviour by placing the bean in the request scope and using either Tomahawk's <t:saveState>, or RichFaces' <a4j:keepAlive>, or JBoss Seam's Conversation Scope, or MyFaces Orchestra.

Again, this problem is not JSF specific. The standard HTTP spec simply doesn't offer any ways to distinguish the client's state (new instance, tab or window) from the server side on.

BalusC
thank you for the response.Can you please tell me, how to define the bean in view scope
Bob
Using `@ViewScoped` or `<managed-bean-scope>view</managed-bean-scope>`. Once again, this is JSF 2.x only. In JSF 1.x you've only `request`, `session`, `application` and `none`.
BalusC
thank you BalusC for your help. One last question, I am using JSF2.0(Mojarra) , and I have a preRenderView event, so will the a4j:keepAlive work ? coz, I tried and I didnt see any effect..
Bob
In Mojarra 2.0 you can put the bean in view scope. Add `@ViewScoped` annotation to the bean class. When you navigate back to the same page after form submits, the bean will be kept alive. Only when you refresh the page or opens a new window/tab, then a new bean will be created. RichFaces is not JSF 2.0 compatible.
BalusC
A: 

First approach and most obvious is to use request scope beans.

But, as previous man described, you can't have new instance of session bean for each browser window.

Andriy Sholokh