动态输入文本字段

Eri*_*ric 2 java jsf primefaces

我正在尝试使用primefaces创建带有标签的动态输入文本字段.
就像我点击添加按钮一样,它应该继续添加标签和输入文本字段.
我可以用什么组件?谢谢.

Bal*_*usC 5

您可以将bean <h:dataTable>@ViewScopedbean 结合使用.

例如

<h:form>
    <h:dataTable id="inputs" value="#{bean.inputs}" var="input">
        <h:column>
            <p:outputLabel for="input" value="#{input.label}" />
        </h:column>
        <h:column>
            <p:inputText id="input" value="#{input.value}" />
        </h:column>
    </h:dataTable>
    <p:commandButton value="Add" action="#{bean.add}" update="inputs" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Input> inputs;

    @PostConstruct
    public void init() {
        inputs = new ArrayList<Input>();
    }

    public void add() {
        Input input = new Input();
        input.setLabel("Input " + (inputs.size() + 1));
        inputs.add(input);
    }

    public List<Input> getInputs() {
        return inputs;
    }

}
Run Code Online (Sandbox Code Playgroud)

public class Input {

    private String label;
    private String value;

    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)

你当然也可以使用a <p:dataTable>,但这只会增加一些花哨的look'n'feel,这对于这个特殊的用例可能是不必要的.

也可以看看: