Welcome to OStack Knowledge Sharing 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 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 OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...