I'm trying to init property before the JSF @PostConstruvtor is called with respect to MVC.
This is my Java code:
@ManagedBean
public abstract class FooController {
protected <type> prop;
public void setProp(<type> prop) {
this.prop = prop;
}
public <type> getProp() {
return this.prop;
}
}
@ManagedBean
public class Foo1Controller extends FooController {
private <otherType> myProp;
@PostConstructor
public void init {
myProp = prop.getProp().getOtherTypeProp();
}
}
[here I have more FooControllers Foo2Controller, Foo3Controller, Foo4Controller...]
@ManagedBean
public class MainController {
// all props have getters and setters
private FooController fooController;
private int controllerType;
private List<SelectItem> myTypes;
private <type> prop;
@PostConstructor
public void init {
// init myTypes here
// init prop here
}
public static Object getBean(String s) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().evaluateExpressionGet(context, "#{" + s + "}", Object.class);
}
public void controllerTypeChange (ValueChangeEvent event) {
controllerType = Integer.valueOf(event.getNewValue().toString());
if (controllerType == 1)
fooController = (Foo1Controller) getBean("foo1Controller");
else if (controllerType == 2)
fooController = (Foo2Controller) getBean("foo2Controller");
....
fooController.setProp(this.prop);
}
}
And this is the XHTML:
<o:SelectOneMenu id="fooType"
value = #{"MainController.controllerType"}
valueChangeListener = "#{MainController.controllerTypeChange}"
styleClass = "dropdown">
<o:ajax action = "#{MainController.controllerTypeChange}">
<f:selectItems value = "MainController.myTypes">
</o:selectOneMenu>
<h:panelGroup id="component1" rendered="#{MainController.controllerType == 1}">
<!-- some component here that uses foo1Controller as it's controller -->
</h:panelGroup>
<h:panelGroup id="component2" rendered="#{MainController.controllerType == 2}">
<!-- some component here that uses foo2Controller as it's controller -->
</h:panelGroup>
The thing is that when i'm creating the foo1Controller in the MainController bean, it's already uses the prop attribute in the foo1Controller @postConstructor but this attribute is NULL because it wasn't yet initialized and i have no idea how to do it before the post constructor is called. The concept behind what i'm tring to do is that MainController can and should have only one child component and they all have a lot in common so it's a must to do here inheritance. When the user select some value in the drop down the relative component should be displayd while the MainController should have a refrence to the component controller.
Any help will be very appreceated. Thanks!