On Thu, 13 Jan 2005 19:11:41 -0600, Eddie Bush <[EMAIL PROTECTED]> wrote: > How does one create managed beans with dynamic values? Would you do > this by listing the managed bean in your faces-config and then having > a Context life cycle listener actually create the beans and put them > in application scope? > > Is there a better practice to follow in doing this?
Yep ... use <managed-property> declarations nested inside your <managed-bean>. As an example of this, the demo program for the Shale proposal (start at http://wiki.apache.org/struts/StrutsShale and follow the links for the nightly builds) uses the following syntax to initialize the dialog controller for the logon process, including turning on or off the "remember me" and "confirmation email" features: <managed-bean> <!-- Basic characteristics of this managed bean --> <managed-bean-name>logon$dialog</managed-bean-name> <managed-bean-class> org.apache.shale.usecases.logon.Dialog </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <!-- Turn off confirmation emails --> <managed-property> <property-name>confirmation</property-name> <property-class>boolean</property-class> <value>false</value> </managed-property> <!-- But turn on 'remember me' cookies --> <managed-property> <property-name>rememberMe</property-name> <property-class>boolean</property-class> <value>true</value> </managed-property> </managed-bean> What's even more interesting, however, is that you can use value binding expressions for the <value> elements ... so you can calculate initialization values dynamically, or even build up a whole tree of beans instead of just one (essentially, managed beans support the "setter injection" style of IoC configuration). For example, consider a case where you want to instantiate a backing bean for a customer, and initialize the credit limit to a value that is based on a context init parameter named "defaultCreditLimit". Something like this will do the trick: <managed-property> <property-name>creditLimit</property-name> <property-class>double</property-class> <value>#{initParam.defaultCreditLimit}</value> </managed-property> because "initParam" is a magic name that returns a Map of the context init parameters. There is also syntax available to initialize maps, lists, and arrays -- see the DTD for a faces-config.xml file for more info. > > Thanks! > Craig --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]