Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

jsf 2 - Dynamically added input field in ui:repeat is not processed during form submit

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.

I have this in the form:

        <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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

You have basically 2 options:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...