-1

在网页中,我<h:dataTable>用来展示它们。

JSF 页面示例:

<h:dataTable value="#{bean.scores}" rowIndexvar="index">
    <h:column>
      <h:outputText value="#{index+1}" />
    </h:column>
    <h:column>
      <h:outputText value="#{score.studentId}" />
    </h:column>
    <h:column>
       <h:inputText value="#{score.teacherScore}" />
    </h:column>
</h:dataTable>

<h:commandButton value="Save" action="#{useMB.save}" />
<h:messages />

这些问题是关于我的 ManagedBean 的:useMB.java

1.我需要编写什么getter和setter方法来将 <h:inputText value="#{score.marks}" /> 值存储在数据库中?

2.如何使用dataTable,JSF和java将同一学科的不同学生成绩保存在数据库中?

3.我需要在xhtml页面做些什么改变?

4

1 回答 1

1

您将需要使用托管 bean 中的集合来保存表中行的值。表中的每一行将代表集合中的单个元素,并且每个元素都可以通过属性dataTable中给出的别名访问。var

<h:dataTable value="#{bean.scores}" rowIndexvar="index" var="score">
    <h:column>
      <h:outputText value="#{index+1}" />
    </h:column>
    <h:column>
      <h:outputText value="#{score.studentId}" />
    </h:column>
    <h:column>
       <h:inputText value="#{score.teacherScore}" />
    </h:column>
</h:dataTable>

在托管 bean 中,您需要有一个元素集合(或数组),每个元素都有具有名称的成员studentIdteacherScore访问器。

public class ManagedBean {
    private Score[] scores;

    public Score[] getScores() { return scores; }

    public void setScores(Score[] scores) {
        this.scores = scores;
    }
}

Score 类应该(至少)如下所示:

public class Score {
    private String studentId;

    private String teacherScore;

    public String getStudentId() { return studentId; }

    public void setStudentId(String studentId) { this.studentId = studentId; }

    public String getTearcherScore() { retyrn teacherScore; }

    public void setTeacherScore(String tearcherScore) { this.tearcherScore = tearcherScore; }
}
于 2013-05-13T11:58:34.863 回答