0

我正在尝试在使用复合组件时设置支持 bean 属性(支持 bean 是一个组合attribute)。

我想有条件地用属性填充一个 bean 属性(例如,finalDraft)(有条件地,因为不需要该属性)。

支持bean:

class Bean ... {
   String finalDraft; // getters+setters

   @PostConstruct
   void init() {
       String draftAttr = (...) getAttributes("draft");
       if(draftAttr!=null) {
           finalDraft = draftAttr;
       }
   }
}

复合材料:

<composite:interface>
    <composite:attribute name="bean" required="true" ... />
    <composite:attribute name="draft" type="java.lang.String" />
</composite:interface>

到目前为止,我尝试使用getAttributes("draft")of @PostConstructbean但它解析为 null (注意:view-scope)。

有没有其他方法可以做到这一点?

4

1 回答 1

1

只是不要将您的 bean 绑定到您的复合组件。而是直接传递属性:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:h="http://java.sun.com/jsf/html">
    <h:body>
        <composite:interface>
            <composite:attribute name="showDraft" default="true" />
            <composite:attribute name="draft" />
        </composite:interface>
        <composite:implementation>
            <h:inputText value="#{cc.attrs.draft}" 
                rendered="#{cc.attrs.showDraft}" />
        </composite:implementation>
    </h:body>
</html>

然后你就可以在你的主页中使用它:

<comp:draftInput value="#{bean.finalDraft}" />

或者

<comp:draftInput showDraft="false" />

恐怕你对这里的 backing bean 感到困惑。一件事是您当前@ViewScopedbean 中的内容,另一件事是复合组件。组合对当前视图的托管 bean 一无所知,它只接收参数并显示它们。它可能是一个单一的 facelet 视图,也可能在他的背后有一个类,它只知道复合,而对范围一无所知。

于 2014-09-10T14:42:45.250 回答