15

私はこの質問をしました、そして答えは私のニーズを直接満たしましたが、私はこの特定の問題のためのより簡単な解決策がなければならないという感覚を残されています。

アイテムのリストを受け入れる複合コンポーネントが欲しい(複合コンポーネント内でメンバーを自由に使用できるように合意されたアイテムのタイプ)

CC(複合コンポーネント)はアイテムのリストを表示し、アイテムの加算と減算を可能にします。

これを最も簡単で効率的な方法で行いたいと思います。

問題を説明するために、例:

ここに画像の説明を入力してください

定義はかなり単純である必要があります(もちろん、そうでない場合を除きます:-)):

<special:dynamicFieldList value="#{bean.fieldList} />

Fieldオブジェクトの最も抽象的な形式は次のとおりです。

public class Field{
 String uuid;
 String value;
}

それだけだと思います。これを簡単な方法でどのように実装しますか?

ありがとう!

4

1 に答える 1

24

の属性でバインドできる<h:dataTable>バッキングを持つ複合コンポーネントで を使用します。バッキングでは、アクションを維持および定義できます。UIComponentcomponentType<composite:interface>UIComponentDataModel

dynamicFieldList.xhtml

<ui:composition
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:cc="http://java.sun.com/jsf/composite"
>
    <cc:interface componentType="dynamicFieldList">
        <cc:attribute name="value" type="java.util.List" required="true" />
    </cc:interface>
    <cc:implementation>
        <h:dataTable id="table" binding="#{cc.table}" value="#{cc.attrs.value}" var="field">
            <h:column><h:outputLabel value="#{field.label}" /></h:column>
            <h:column><h:inputText value="#{field.value}" /></h:column>
            <h:column><h:commandButton value="remove" action="#{cc.remove}" /></h:column>
        </h:dataTable>
        <h:commandButton value="add" action="#{cc.add}" />
    </cc:implementation>
</ui:composition>

(<h:inputText>必要に応じて、複合フィールド コンポーネントにすることができます)

com.example.DynamicFieldList

@FacesComponent(value="dynamicFieldList") // To be specified in componentType attribute.
@SuppressWarnings({"rawtypes", "unchecked"}) // We don't care about the actual model item type anyway.
public class DynamicFieldList extends UINamingContainer {

    private UIData table;

    public void add() {
        ((List) getAttributes().get("value")).add(new Field("somelabel"));
    }

    public void remove() {
        ((List) getAttributes().get("value")).remove(table.getRowData());
    }

    public UIData getTable() {
        return table;
    }

    public void setTable(UIData table) {
        this.table = table;
    }

}

次のように使用します。

<h:form>
    <my:dynamicFieldList value="#{bean.fields}" />
</h:form>

これだけで

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Field> fields;

    public Bean() {
        fields = new ArrayList<>();
    }

    public List<Field> getFields() {
        return fields;
    }

}

public class Field implements Serializable {

    private String label;
    private String value;

    public Field() {
        //
    }

    public Field(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}
于 2011-06-15T13:18:26.050 に答える