Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Obtaining an instance of a Managed Bean declared with Application Level Scope

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a managed bean that i have defined with Application Level Scope that means it only gets instantiated once during the life
of the application, Now i would like to get a reference to that instance from my Managed bean which has Session Level Scope, Please how do i go about this, and i would not want to say A ab=new A(); as this would create another instance
 
Ryan Lubke
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abiodun Adisa:
I have a managed bean that i have defined with Application Level Scope that means it only gets instantiated once during the life
of the application, Now i would like to get a reference to that instance from my Managed bean which has Session Level Scope, Please how do i go about this, and i would not want to say A ab=new A(); as this would create another instance


Say your application scoped managed bean is called 'app'.

Then in your session managed bean definition you'd:



When 'sessBean' is instaniated by JSF, the application scoped bean will
be injected into the instance. You need to make sure there is public property accessors for this to work of course.
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan Lubke:


Say your application scoped managed bean is called 'app'.

Then in your session managed bean definition you'd:



When 'sessBean' is instaniated by JSF, the application scoped bean will
be injected into the instance. You need to make sure there is public property accessors for this to work of course.



Hello,
I did it as you suggested but it did'nt work, I am posting my code probably i missed something

Faces-Config.xml

<faces-config xmlns="http://java.sun.com/JSF/Configuration">
<managed-bean>
<managed-bean-name>backing_index</managed-bean-name>
<managed-bean-class>view.backing.Index</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>Test</property-name>
<value>#{Testing}</value>
</managed-property>
</managed-bean>

<managed-bean>
<managed-bean-name>backing_content</managed-bean-name>
<managed-bean-class>view.backing.Content</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/content.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>Testing</managed-bean-name>
<managed-bean-class>view.backing.Testing</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
</faces-config>

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
and for the accessors

public void setTest(Testing test) {
this.Test = test;
//System.out.println("Session Bean Injectd with Test"+Test.result);
}

public Testing getTest() {
return Test;
}

but then i kept getting this error

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

javax.servlet.jsp.JspException: javax.faces.FacesException: javax.faces.FacesException: Can't set managed bean property: 'Test'.at com.sun.faces.taglib.html_basic.FormTag.doStartTag(FormTag.java:355)at _index._jspService(_index.java:60)[/index.jsp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic