2013-05-13 39 views
-1

在網頁中,我用<h:dataTable>來表示它們。 JSF頁面的如何在數據庫中保存多個DATATABLE的h:inputText值

例子:

<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頁面?

回答

1

您將需要使用託管bean中的集合來保存表中行的值。表中的每一行都將表示集合中的單個元素,並且每個元素都可通過dataTable中的屬性中給出的別名進行訪問。

<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> 

在Managed Bean,你將需要有元素的集合(或陣列),他們每個人具有名字studentIdteacherScore和存取他們的成員。

public class ManagedBean { 
    private Score[] scores; 

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

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

比分類應該看(至少)是這樣的:

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; } 
} 
+0

感謝,現在它工作正常。 – 2013-05-15 05:41:30

相關問題