jsf - 如何使用 JSF 2.0 复合组件实现动态列表?

标签 jsf jsf-2 facelets dynamic-forms composite-component

我问 this问题,尽管答案直接满足了我的需求,但我觉得必须为这个特定问题提供更简单的解决方案。

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

CC(复合组件)显示项目列表并允许添加和减去项目。

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

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

enter image description here

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

<special:dynamicFieldList value="#{bean.fieldList} />
Field 的最抽象形式对象将是:
public class Field{
 String uuid;
 String value;
}

我想就是这样。
您将如何以简单的方式实现这一点?

谢谢!

最佳答案

我会使用 <h:dataTable>在带有背衬的复合组件中 UIComponent您可以通过 componentType 绑定(bind)<composite:interface> 的属性.在后台UIComponent然后您可以维护 DataModel并定义 Action 。
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;
    }

}

关于jsf - 如何使用 JSF 2.0 复合组件实现动态列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6358066/

相关文章:

java - Helloworld JSF2.0 页面未呈现为 html 标签

jsf - JSF:检查=“id”/> <h:message是否存在的更好方法

jsf - 定义<ui :fragment> tag in jsf有什么用

jsf-2 - 外部 facelets 模板 - 它是否被缓存?

java - JSF 不会在 setter 和 getter 中获取数据(CRUD 应用程序)

jsf - 如何使用 <f :ajax> to set updated value in managed bean when value of <h:inputText> is changed

javascript - 切换 <p :selectCheckboxMenu> items in JavaScript

javascript - 延迟加载和解析 PrimeFaces JavaScript 文件

jsf-2 - 何时使用 <ui :include>, 标记文件、复合组件和/或自定义组件?

java - 隐藏 p :schedule with css 中的事件