p:collector 与自定义包装器 class 的用例是什么?

What's the use case of p:collector vs custom wrapper class?

我不知道 p:collector。我创建了这个自定义摘要 class:

abstract public class Wrapper<T> {
    protected Integer number;
    protected List<T> list;
    protected Class<T> klass;
    
    public Wrapper() {
        this.setList(new ArrayList<T>());
    }
    
    public void setListNumber(Integer number) {
        Class<T> klass = this.getKlass();
        List<T> list = this.getList();
        ListUtil.setSize((ArrayList<T>) list, number, klass);
    }
    
    public void add() {
        List<T> list = this.getList();
        Class<T> klass = this.getKlass();
        try {
            list.add(klass.newInstance());
        }
        catch (InstantiationException e) {
            throw new InstantiationUncheckedException(e);
        }
        catch (IllegalAccessException e) {
            throw new IllegalAccessUncheckedException(e);
        }
    }
    
    public void remove(int i) {
        List<T> list = this.getList();
        list.remove(i);
    }
    
    
    
    public Integer getNumber() {
        return number;
    }
    
    public void setNumber(Integer number) {
        this.number = number;
    }
    
    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Class<T> getKlass() {
        return klass;
    }

    public void setKlass(Class<T> klass) {
        this.klass = klass;
    }
}

我通常这样使用它:

public class Things extends Wrapper<Thing> {
    public Things() {
        super();
        this.setKlass(Thing.class);
    }

    @Override
    public String toString() {
        return "Things [number=" + number + ", list=" + list + "]";
    }
}

并在视图中,例如:

<ui:repeat varStatus="status" var="thing" value="#{bean.things.list}">

<p:commandButton 
    value="Add"
    action="#{bean.things.add()}"
    immediate="true"
/>

<p:commandButton 
    value="❌"
    action="#{bean.licenze.remove(status.index)}"
    immediate="true"
/>

p:collector 收集器可以做到这一点 better/with 更少的代码?

p:collectorremoved in PrimeFaces 11 after being deprecated in 10,所以如果您想升级,最好不要使用它。