EngTiyeb
EngTiyeb

Reputation: 38

How does the Scope affect the objects of the Bean?

I was reading this entire introduction to understand when and why we should choose a specific Scope, it is clear for static fields and methods, but for the objects of the Bean itself it is not, at least for me.

Bean1:

@ManagedBean
@SessionScoped
public class ConsultBean   implements java.io.Serializable {
    public  String name="oldName";
    
    public String getResults(){
    return "index";  
    }
    ..
      

Bean2:

@ManagedBean
@SessionScoped
public class TestBean   implements java.io.Serializable {
    public  ConsultBean obj=new ConsultBean();
    
     public String show(){
     obj.setName("newName");
     return obj.getResults(); 
    }
    ..

index.jspx/xhtml

<h:panelGrid  columns="2">
<h:outputText value="Result:"/>
<h:outputText value="#{consultBean.name}"/> 
</h:panelGrid>

The result was: oldName!

But when:

public String getResults(){
    this.setName("New Name")
    return "index";  
    }

The result is STILL: oldName!!

Does the object where the call initiated matter?

Upvotes: 0

Views: 30

Answers (1)

Ramanlfc
Ramanlfc

Reputation: 8354

The problem is public ConsultBean obj=new ConsultBean();, you are not supposed to instantiate managed beans. It's the job of the framework to do it for you, that's the whole point of managed beans.

Inject ConsultBean with ManagedProperty.

Although, I would suggest you to use CDI instead of the old jsf bean api

Upvotes: 2

Related Questions