我有一个代码允许老师添加学生并查看他们,但是每次我添加一个学生时,前一个学生也会被覆盖。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<h:form>
<h1><label>Student Grades</label></h1>
<hr />
<h:dataTable value="#{course.studentList}" var="st" border="1" rendered="#{course.render}">
<f:facet name="caption">#{course.courseCode} #{course.courseName}</f:facet>
<h:column>
<f:facet name="header">Student Name</f:facet>
<h:outputText value="#{st.studentName}" />
</h:column>
<h:column>
<f:facet name="header">Score</f:facet>
<h:outputText value="#{st.studentScore}" />
</h:column>
</h:dataTable><br />
<label>Student Name</label>
<h:inputText id="studentName" label="Student Name" value="#{student.studentName}" required="true" requiredMessage="Please enter a student name">
</h:inputText>
<h:message for="studentName" /><br />
<label>Student Score</label>
<h:inputText id="studentScore" label="Student Score" value="#{student.studentScore}" required="true" converterMessage="You must enter a number">
<f:validateDoubleRange minimum="0" maximum="100" />
</h:inputText>
<h:message for="studentScore" /><br />
<h:commandButton value="Add Grade" action="#{course.add(student)}"/>
<h:commandButton value="Submit" action="summary"/>
</h:form>
</h:body>
</html>
下面是student.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "student")
@SessionScoped
public class Student {
private String studentName;
private Double studentScore;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Double getStudentScore() {
return studentScore;
}
public void setStudentScore(Double studentScore) {
this.studentScore = studentScore;
}
}
下面是 Course.java,它有一个学生数组列表。
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
@ManagedBean(name = "course")
@SessionScoped
public class Course {
private String courseName;
private String courseCode;
private ArrayList<Student> studentList = new ArrayList<Student>();
private double courseAverage;
private boolean render;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
public ArrayList<Student> getStudentList() {
return studentList;
}
public void setStudentList(ArrayList<Student> studentList) {
this.studentList = studentList;
}
public double getCourseAverage() {
int i = 0;
for (Student st : studentList) {
courseAverage += st.getStudentScore();
i++;
}
return courseAverage/i;
}
public void setCourseAverage(double average) {
this.courseAverage = average;
}
public boolean isRender() {
return render;
}
public void setRender(boolean render) {
this.render = render;
}
public String add(Student student) {
studentList.add(student);
render = true;
return null;
}
public String reset() {
studentList = new ArrayList<Student>();
courseName = "";
courseCode= "";
courseAverage = 0.0;
render = false;
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "home";
}
public boolean renderable() {
boolean render = false;
for (Student st : studentList) {
render = true;
}
return render;
}
}