2

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!

DiSol
  • 414
  • 2
  • 11
  • first: to get useful answers you should post a [SSCCE](http://sscce.org/) and your code now is not self-contained and it also has syntax errors. Second: explain your problem and what do you want to achieve, you are just describing the code you posted, which is not self-contained and not compilable so you your question is difficult to understand. Now, what I see in your code is that your `FooXController` never initialises the `prop` property (not even at `MainController` but tries to initialise the `myProp` using the uninitialised value from it. – Alonso Dominguez Apr 08 '13 at 14:24
  • @Alonso Dominguez, my real classes too big and i can't post them so I tried to extract what seemes important for describing the issue, if there is unclear things - please ask and I'll explain. Regarding to the thing that the FooXController never initialize the prop: The prop value should be initialized in the MainController and the FooXController is taking some other property from the prop in the PostConstructor but the problem is that when I'm creating a FooXController bean it's automaticly calling the PostConstructor which need to use the prop but the MainController couldn't still init it. – DiSol Apr 09 '13 at 08:32
  • sorry for being too harsh, but if even the little code you post is not compilable is quite difficult to know where the problem is. I know that you are trying to initialise the `prop` property in your `FooXController` using the one from the `MainController`, I can see that, but I'm not seeing where are you initialising the `MainController`'s one if you are initialising it (your `init` methods are empty and they don't even have brackets!). Please, try to post a SSCCE, follow the instructions to trim your code and you may find the solution to your issue even before asking for it... – Alonso Dominguez Apr 09 '13 at 09:46
  • @Alonso Dominguez, I updated the code and the MainController initializes the prop value in the PostConstructor (it's actually taking values from DB - hibrenate). – DiSol Apr 09 '13 at 11:12
  • If `` a `Managedbean`? If so, just use the `@ManagedProperty` so the `prop` property will be injected in the `FooController`. If not, you cannot use the `@PostConstruct` (without 'er') because it will be called initially after the `ManagedBean` has been baked, which happens at a time where you didnt have chance to call the `foo1Controller.setProp()` method. – Manuel Jun 06 '13 at 07:53

1 Answers1

0

define servlet which should GenericServlet in your application do whatever you want. and define it in web.xml set load-on-startup tag. Sample code for servlet

public class ResourceInitializer extends GenericServlet {

@Override
    public void init(final ServletConfig config) throws ServletException {
        super.init(config);
// YOUR APPLICATION INIT CODE
}

@Override
    public void service(final ServletRequest req, final ServletResponse res)
            throws ServletException {
    }

    @Override
    public void destroy() {
        // tell the ResourceManager to cleanup and remove the resouces
        LibraOfficeService.getInstance().closeConnection();
        super.destroy();
    }
}

in the web.xml

 <servlet>
    <description>Initializes Resources</description>
    <servlet-name>ResourceInitializer</servlet-name>
    <servlet-class>packageNAME.ResourceInitializer</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
Umair
  • 860
  • 2
  • 13
  • 30