2

我试图在相对于 MVC 调用 JSF @PostConstruvtor 之前初始化属性。

这是我的 Java 代码:

@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);
    }
}

这是 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>

问题是,当我在 MainController bean 中创建 foo1Controller 时,它已经使用了 foo1Controller @postConstructor 中的 prop 属性,但是该属性为 NULL,因为它尚未初始化,我不知道如何在后构造函数被调用。我要做的事情背后的概念是 MainController 可以而且应该只有一个子组件,它们都有很多共同点,因此必须在这里进行继承。当用户在下拉列表中选择某个值时,应显示相关组件,而 MainController 应具有对组件控制器的引用。

任何帮助都会非常感激。谢谢!

4

1 回答 1

0

定义 Servlet 应该让应用程序中的GenericServlet做任何你想做的事情。并在 web.xml set load-on-startup 标签中定义它。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();
    }
}

在 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>
于 2015-03-27T05:32:51.707 回答