Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
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" />

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 + "");

    return null;

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

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

    for (int i = 0; i < 4; i++) {
See Question&Answers more detail:os

Welcome To Ask or Share your Answers For Others

1 Answer

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:

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share