Search code examples
xpagesmanaged-bean

Binding an edit box control to a specific Vector element in managed bean?


I have a Vector of Scores for test grades, as a property of my Assignment class.

I have an Edit Box on an xpage where I want to edit the value and write back to the Vector in the managed bean. The value binding of the Edit Box is: <xp:this.value><![CDATA[#{rpt.scores[rowIndex]}]]></xp:this.value> Where rpt is the Assignment object from my bean. The Edit Box is in a repeat control because I don't know how many students will be taking the test each time. So I am using the rowIndex of the repeat control to identify which element of the Scores Vector I want to bind to.

It is reading the value from the Scores Vector correctly, but I cannot seem to change the value and have it written back to the Vector.

How do I get it to write the value back to the Scores[n] element of the Assignment class?

here is the Assignment Class from my bean:

package com.logickey.gradebook;

import java.io.Serializable;
import java.util.Vector;


public class Assignment implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -1500382996574385949L;
    private String Name;
    private String Teacher;
    private String AssignNum;
    private String AssignDate;
    private Vector<String> Scores;

    public Assignment() {
        Name = "";
        Teacher = "";
        AssignNum = "";
        AssignDate = "";
        Scores = new Vector<String>();
    }
    public Assignment(String name, String teacher, String assignNum, String assignDate, Vector<String> scores){
        Name = name;
        Teacher = teacher;
        AssignNum = assignNum;
        AssignDate = assignDate;
        Scores = scores;
    }


    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getTeacher() {
        return Teacher;
    }

    public void setTeacher(String teacher) {
        Teacher = teacher;
    }

    public String getAssignNum() {
        return AssignNum;
    }

    public void setAssignNum(String assignNum) {
        AssignNum = assignNum;
    }

    public String getAssignDate() {
        return AssignDate;
    }

    public void setAssignDate(String assignDate) {
        AssignDate = assignDate;
    }


    public Vector<String> getScores() {
        return Scores;
    }

    public void addScore(String input) {
        if (Scores==null) {
            Scores = new Vector<String>();
        }
        Scores.add(input);
    }


}

Solution

  • Per is right. However I would suggest to take it one step further. Your scores Vector could be a class of its own including student name and score.

    you need a getScores() and setScores(Vector newValues) method. The repeat control will take care to insert at the right position.

    If you use a custom class you need get/set methods on it. then you can bind fields e. g. rpt. Student

    You also might consider to visit the Collection framework to see if there is a better fit:

    • Vector keeps the insert order
    • Set : ensures no duplicates (implement Comparble)
    • Tree Set : keeps stuff sorted automatically

    There are more, have fun!

    Let us know how it goes