如何使用JSF 2.0 Composite Component实现动态列表?

Ben*_*Ben 14 jsf facelets dynamic-forms composite-component jsf-2

我问了这个问题,虽然答案直接满足了我的需求,但我仍然觉得必须有一个更简单的解决方案来解决这个问题.

我想有一个复合组件接受一个项目列表(商定的项目类型,以便成员可以在复合组件中自由使用)

CC(复合组件)显示项目列表并允许项目的加法和减法.

我想以最简单有效的方式做到这一点.

为了说明问题,一个例子:

在此输入图像描述

定义应该相当简单(当然,除非:-)):

<special:dynamicFieldList value="#{bean.fieldList} />
Run Code Online (Sandbox Code Playgroud)

Field对象最抽象的形式是:

public class Field{
 String uuid;
 String value;
}
Run Code Online (Sandbox Code Playgroud)

我猜就是这样.你会如何以简单的方式实现这一点?

谢谢!

Bal*_*usC 24

<h:dataTable>在一个带有支持的复合组件中使用a UIComponent,你可以通过它的componentType属性绑定<composite:interface>.在后台,UIComponent您可以维护DataModel并定义操作.

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>
Run Code Online (Sandbox Code Playgroud)

(<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;
    }

}
Run Code Online (Sandbox Code Playgroud)

使用方法如下:

<h:form>
    <my:dynamicFieldList value="#{bean.fields}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

就这样

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Field> fields;

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

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

}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

  • 好一个!:)我想知道复合组件中的嵌入表单.给定方便,但也意味着此特定组件不能在另一种形式中使用. (3认同)
  • @Arjan:将表格保留在外面确实更有意义.我将编辑该示例. (2认同)