-
1. Re: How to Dynamicly inject a bean
niox.nikospara.yahoo.com Jul 15, 2009 11:20 AM (in response to ea87)You could use the factory pattern. It is implemented in SEAM by annotating a method with @Factory. Your method will somehow sense the screen you are currently on (there are many ways to do this) and return the appropriate subclass of A. Take care to place the returned value in the correct context!
Read more in the section entitled
Factory and manager components
in the documentation. -
2. Re: How to Dynamicly inject a bean
ea87 Jul 15, 2009 3:51 PM (in response to ea87)Thank you Nikos, looks like just the thing i was looking for,
anyway about the:
Nikos Paraskevopoulos wrote on Jul 15, 2009 11:20:
Your method will somehow sense the screen you are currently on (there are many ways to do this)I'm sure there are many ways, but i'm kind of new in the JEE/SEAM programming world, so could please give me a hint about what's a good way to do this?,
thanks again. -
3. Re: How to Dynamicly inject a bean
niox.nikospara.yahoo.com Jul 15, 2009 5:01 PM (in response to ea87)Hello again,
First thing that comes to mind is using the view id: FacesContext.getCurrentInstance().getViewRoot().getViewId(). That is pretty simple but dirty, since you need to specify the view id explicitly.
A more JSF-ish way is this: Create the A implementation (instance of B or C) from the action method that navigates to pageB or pageC. Then set it into the component that contains the factory method. E.g.:
// component that contains the factory method @Name("factoryComp") public class FactoryComp { private A alphaImpl; @Factory("alphaImpl") public A getAlphaImpl() { return alphaImpl; } public void setAlphaImpl(A a) { alphaImpl = a; } }
// component containing action method @Name("actionComp") public class ActionComp { @In FactoryComp factoryComp; public String actionMethod() { A alphaImpl; String result = null; if( ...conditions... ) { alphaImpl = new B(); result = "pageB"; } else if( ...conditions... ) { alphaImpl = new C(); result = "pageC"; } factoryComp.setAlphaImpl(alphaImpl); return result; } }
Finally you can use a page action, different for pages B and C. The page action of pageB will create a new B() and set it into the factory. Same for action of pageC.