myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Myfaces Wiki] Update of "FAQ" by SimonKitching
Date Sun, 13 Nov 2005 20:00:28 GMT
Dear Wiki user,

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

The following page has been changed by SimonKitching:
http://wiki.apache.org/myfaces/FAQ

The comment on the change is:
Add entry on locating a managed bean from another managed bean

------------------------------------------------------------------------------
  indicate a binary-compatible release, as the JSF specification number has not changed. All
code using JSF-specified features will continue
  to work as previously (unless the previous behaviour was buggy).
  
+ === How can I access one Managed Bean from another? ===
+ 
+ There are two ways for one managed bean to access another managed bean in the same webapp:
+ 
+ ==== 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
+ 
+ ==== Using Lookup ====
+ 
+ The following java code can be used to explicitly look up an arbitrary managed bean by name:
+ {{{
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ NeededBean neededBean
+     = (NeededBean) facesContext.getApplication().getVariableResolver()
+       .resolveVariable(facesContext, "neededBean");
+ }}}
+ 

Mime
View raw message