表单提交期间未处理ui:repeat中动态添加的输入字段 [英] Dynamically added input field in ui:repeat is not processed during form submit

查看:99
本文介绍了表单提交期间未处理ui:repeat中动态添加的输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为应用程序中的答案创建一个输入表单,并且从四个空"答案开始,这些答案会在视图中循环并为其输入字段.我有一个添加答案按钮,我将一个问题添加到答案数组中,然后视图再次呈现答案,但现在有了一个附加的输入字段.支持bean已进行了范围分析.但是,如果我提交表单时没有按添加答案按钮,则一切正常.数据保存在数据库中.但是,如果我在填写完四个答案后添加答案,则最后一个答案不会从输入字段(answer.description)中获取数据.如果我先按添加答案(不填写任何输入字段),则根本不会捕获这些字段中的数据,而将全部5个留空,因此数据库中不会保存任何数据.

I am trying to make a input form for answers in my application and I start with four "empty" answers which the view loops over and make input fields for. I have an add answer button which I add one question to the array of answers and then the view render the answers again, but now with an additional input field. The backing bean is viewscoped. However if I submit the form without pressing the add answer button it all works. The data is saved in the database. But if I add an answer after the four is filled out the last one does not get the data from the inputfield (answer.description). If I press the add answer first (without filling out any input fields) the data from the fields are not captured at all leaving all 5 empty so no data is saved in the database.

我的格式为:

        <ui:repeat var="answer" value="#{bean.answers}">
            <div class="field">
                <h:outputLabel for="answerAlternative-#{answer.serialNumber}"
                    value="Svaralternativ #{answer.serialNumber}" />
                <h:inputText id="answerAlternative-#{answer.serialNumber}"
                    value="#{answer.description}" size="40" />
            </div>
        </ui:repeat>

这是用于创建新输入字段的方法:

This is the method for creating a new input field:

public String addAnswer() {
    if (answers.size() + 1 < 6) {
        Answer answer = new Answer();
        answer.setSerialNumber(answerSerialNumber + "");
        answerSerialNumber++;
        answers.add(answer);
    }

    return null;
}

用于初始化具有四个空输入字段的答案数组:

Used for initializing the answers array with four empty input fields:

@PostConstruct
public void initBean() {
    answers = new ArrayList<Answer>();

    for (int i = 0; i < 4; i++) {
        addAnswer();
    }
}

推荐答案

此外观与Mojarra中<ui:repeat>的当前问题相匹配.它是

This look to match the current problems of <ui:repeat> in Mojarra. It is totally broken in Mojarra.

您基本上有2个选择:

  • Replace Mojarra by MyFaces which has a way more stable implementation of <ui:repeat>.
  • Use an UIData component instead of <ui:repeat>, e.g. <h:dataTable>, Tomahawk's <t:dataList>, PrimeFaces' <p:dataList>, etc.

这篇关于表单提交期间未处理ui:repeat中动态添加的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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