obinini
obinini

Reputation: 121

how can i handle undefined number of related inputText in JSF

i need to collect homework scores of students in a class. what i have done so far is

  1. get the list of student ids
  2. using ui:repeat i loop thru the list and for each student id, i

    2a)display an h:inputText whose value is the current student id, then

    2b)to the right of textbox in (2a) above, i display another h:inputText for the teacher to enter the score for that student(for now that value is a dummy variable just to get the page to display).

  3. i have a single commandbutton to submit all the data.

For example, if i have 20 students, i would have 20 rows, where each row has two h:inputText, one already containing the current student_id and the other is empty for the teacher to type the score.

How can i collect these values correctly, so that the right student id is linked to the right score.

Note that i cant hardcode the number of textfields cos the number of students in a class can change at any time.

Upvotes: 1

Views: 211

Answers (1)

BalusC
BalusC

Reputation: 1108722

Create a model object.

public class Score {

    private Long studentId;
    private BigDecimal teacherScore;

    // Getters/setters.
}

Have a list of them in some JSF managed bean.

private List<Score> scores;

Use <h:dataTable> to present them.

<h:dataTable value="#{bean.scores}" var="score">
    <h:column><h:inputText value="#{score.studentId}" /></h:column>
    <h:column><h:inputText value="#{score.teacherScore}" /></h:column>
</h:dataTable>
<h:commandButton value="Save" action="#{bean.save}" />
<h:messages />

That's it.

Upvotes: 2

Related Questions