如何收集List< T>的提交值.在JSF中? [英] How to collect submitted values of a List<T> in JSF?

查看:117
本文介绍了如何收集List< T>的提交值.在JSF中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List<T>的豆:

@Named
@ViewScoped
public class Bean {

    private List<Item> items;
    private String value;

    @Inject
    private ItemService itemService;

    @PostConstruct
    public void init() {
        items = itemService.list();
    }

    public void submit() {
        System.out.println("Submitted value: " + value);
    }

    public List<Item> getItems() {
        return items;
    }
}

我想编辑每个项目的value属性:

And I'd like to edit the value property of every item:

<h:form>
    <ui:repeat value="#{bean.items}" var="item">
        <h:inputText value="#{bean.value}" />
    </ui:repeat>
    <h:commandButton action="#{bean.submit}" />
</h:form>

使用此代码,value并不包含所有提交的值,而仅包含最新提交的值.我也尝试了<c:forEach><h:dataTable>,但是没有任何区别.

With this code the value doesn't contain all the submitted values, only the latest submitted value. I also tried <c:forEach> and <h:dataTable>, but it didn't made any difference.

如何收集所有提交的值?

What should I do for collecting all the submitted values?

推荐答案

引起问题的原因是,您基本上将所有提交的值收集到一个相同的 bean属性中.您需要将value属性移到var="item"后面的bean中.

Your problem is caused because you're basically collecting all submitted values into one and same bean property. You need to move the value property into the bean behind var="item".

<h:form>
    <ui:repeat value="#{bean.items}" var="item">
        <h:inputText value="#{item.value}" /> <!-- instead of #{bean.value} -->
    </ui:repeat>
    <h:commandButton action="#{bean.submit}" />
</h:form>

在bean操作方法中,只需遍历items即可通过item.getValue()获取所有提交的值.

In the bean action method, simply iterate over items in order to get all submitted values via item.getValue().

public void submit() {
    for (Item item : items) {
        System.out.println("Submitted value: " + item.getValue());
    }
}

这篇关于如何收集List&lt; T&gt;的提交值.在JSF中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆