You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by Apache Wiki <wi...@apache.org> on 2011/08/04 00:14:23 UTC

[Myfaces Wiki] Update of "AccessingOneManagedBeanFromAnother" by LeonardoUribe

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The "AccessingOneManagedBeanFromAnother" page has been changed by LeonardoUribe:
http://wiki.apache.org/myfaces/AccessingOneManagedBeanFromAnother?action=diff&rev1=4&rev2=5

- === How can I access one Managed Bean from another? ===
+ See the updated page here:
  
- There are two ways for one managed bean to access another managed bean in the same webapp:
+ https://cwiki.apache.org/confluence/display/MYFACES/Accessing+one+managed+bean+from+another
  
- ==== Using Dependency Injection ====
- 
- In your project's faces configuration file which defines the managed beans, a managed bean
- property can be declared as initialised with a reference to another managed bean:
- 
- {{{
-  <managed-bean>
-    <managed-bean-name>neededBean</managed-bean-name>
-    <managed-bean-class>fqn.to.NeededBean</managed-bean-class>
-    <managed-bean-scope>session</managed-bean-scope>
-  </managed-bean>
- 
-  <managed-bean>
-    <managed-bean-name>usingBean</managed-bean-name>
-    <managed-bean-class>fqn.to.UsingBean</managed-bean-class>
-    <managed-bean-scope>request</managed-bean-scope>
-    <managed-property>
-      <property-name>neededBean</property-name>
-      <value>#{neededBean}</value>
-    </managed-property>
-  </managed-bean>
- }}}
- 
- The constraints are that:
-  * the using bean must have scope which is the same as or shorter than the needed bean
-  * the using bean must have a property-setter method which takes the needed bean as a parameter
-  * the beans cannot have managed dependencies on each other.
- 
- ==== Using Lookup ====
- 
- The following java code can be used in MyFaces 1.1 to explicitly look up an arbitrary managed bean by name:
- {{{
- FacesContext facesContext = FacesContext.getCurrentInstance();
- NeededBean neededBean
-     = (NeededBean) facesContext.getApplication()
-         .getVariableResolver().resolveVariable(facesContext, "neededBean");
- }}}
- In MyFaces 1.2, that code is deprecated, and preffered version is:
- {{{
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
- NeededBean neededBean 
-     = (NeededBean) FacesContext.getCurrentInstance().getApplication()
-         .getELResolver().getValue(elContext, null, "neededBean");
- }}}
- Alternately, you can use this code to evaluate any JSF EL expression.
- {{{
- FacesContext facesContext = FacesContext.getCurrentInstance();
- NeededBean neededBean
-     = (NeededBean)facesContext.getApplication()
-       .createValueBinding("#{neededBean}").getValue(facesContext);
- }}}
-