SE成長痛日記

アラサーSEが技術力を高めるために日々感じる痛みを綴るBlogです。

複合コンポーネントのインターフェイスに何が渡せるか

表題どおりです。

複合コンポーネントインターフェイスに何が渡せるのか調査してみます。

前回は、3つのString型でしたが、 オブジェクトそのものが渡せると、夢がきっと広がります。

まず次のように定義して受け取ることができるか確認します。

    <composite:interface>
        <composite:attribute name="name"/>
        <composite:attribute name="mail"/>
        <composite:attribute name="body"/>
        <composite:attribute name="responce"/>
    </composite:interface>
<comp:respons name="#{respon.name}" mail="#{respon.mail}" body="#{respon.body}" responce="#{respon}"/>

f:id:hirohisoEx:20190323135606p:plain 特に何事もなく、実行できました。

次に、オブジェクトのメンバにアクセスしてみます。

    <composite:interface>
        <composite:attribute name="name"/>
        <composite:attribute name="mail"/>
        <composite:attribute name="body"/>
        <composite:attribute name="responce" />
    </composite:interface>

    <composite:implementation>      
        <div>
            <h:outputText value="#{cc.attrs.name}"/>
            <h:outputText value="#{cc.attrs.mail}"/>
            <h:outputText value="#{cc.attrs.responce.body}"/>
        </div>       
    </composite:implementation>

こんな感じで、一つだけ書き換えてみました。 f:id:hirohisoEx:20190323140046p:plain 普通にうまくいきました。 どんな仕組みかわかりませんが、複合コンポーネントで受け取ったオブジェクトの型を うまく識別しています。

なので、全体的に次の様に書き直しました。 これでオブジェクトそのものを複合コンポーネントに渡せるため、呼び出し側は非常にすっきりします。 また、オブジェクトのメンバが増えても複合コンポーネントの定義側だけを書き直せばよく。 呼び出し側には手を加える必要がないのもメリットとして大きいです。

        <ui:repeat var="respon" value="#{responseBean.responseList}" >
            <comp:respons  responce="#{respon}"/>
        </ui:repeat>
    <composite:interface>
        <composite:attribute name="responce" />
    </composite:interface>

    <composite:implementation>      
        <div>
            <h:outputText value="#{cc.attrs.responce.name}"/>
            <h:outputText value="#{cc.attrs.responce.mail}"/>
            <h:outputText value="#{cc.attrs.responce.body}"/>
        </div>       
    </composite:implementation>

本日はここまで。