java - Arraylist last element overwriting previous elements -


i have code allows teacher add students , view them, everytime add student, previous student overwritten well.

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

below 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; } } 

below course.java has arraylist of students.

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 = 0;     (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;     (student st : studentlist) {         render = true;     }     return render; } } 

as student class annotated @sessionscoped @managedbean, created , stored throughout user's http session, i.e. unless invalidated, or expires. so, when add value list add same reference everywhere , modify fields of student object once created , later fields continuously replaced and, thus, every list element contains reference the same student object, modified well.

what need create brand new student object added list , removing bean annotations it.

as side note, sure understand differencebetween , use cases of managed bean , model/placeholder before proceed further.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -