Akvel
Akvel

Reputation: 951

JSF 2.0 Managed Property object has two different instances, when I to open page and to do ajax request

all

I novice in JSF2 (used Mojarra + primeFaces on tomcat7) and I get strange behavior of ManagedProperty object:

@ManagedBean
@ViewScoped
public class CreateFactMB implements Serializable{

    @ManagedProperty(value="#{collectionFactTable}") 
    private CollectionFactTable collectionFactTable; //SessionBean
    ...
    //setters/getters

I printed object when I open page (refresh brouser) I see one instance of collectionTree

mbeans.CollectionFactTable@12803ba

But when I do ajax request

<p:commandButton id="btn1" value="Save" update="growl"
                actionListener="#{createFactMB.doUpdate}" />    

In doUpdate I see another instance of my collectionTree

mbeans.CollectionFactTable@625c49

It's problem because I can not do change while ajax action (because I have just copy)

Anybody can help me? What I'M doing not right?

Upvotes: 3

Views: 1695

Answers (1)

maple_shaft
maple_shaft

Reputation: 10463

I think you have a misunderstanding of how SessionScoped persistence works in JSF. This behavior is expected and normal.

enter image description here

At the beginning of the request all of the managed beans are instantiated regardless of scope. In the Restore View phase, the session based persistence values are set to the new managed bean object, effectively restoring the SessionScoped bean back to its last state before the last Response was sent.

Once the Response is complete and has been sent the data in these managed bean instances is persisted and the objects dereferenced for garbage collection. The process begins anew at the next request, regardless if it is Ajax or not.

Upvotes: 1

Related Questions