Search code examples
formsjspstrutsstruts-1

Struts 1 : Put values of a jsp into form that uses a java List


In my jsp, I have some fields like this :

<html:text property="field[0]" value="${fieldValue}" indexed="true">
<html:text property="field[1]" value="${fieldValue}" indexed="true">
<html:text property="field[2]" value="${fieldValue}" indexed="true">
<html:text property="field[3]" value="${fieldValue}" indexed="true">

And in my form I have a java.util.list that I need to populate from the fields on top :

private List<Double> field = new ArrayList<Double>();

public final List<Double> getField() {
    return field;
}
public final void setField(final List<Double> valeur) {
    this.field = valeur;
}

The problem is that the list is not populated. Any ideas ??? Thanks !!


Solution

  • Simply do this

    <html:text property="field[0]" value="${fieldValue}" indexed="true">
    <html:text property="field[1]" value="${fieldValue}" indexed="true">
    <html:text property="field[2]" value="${fieldValue}" indexed="true">
    <html:text property="field[3]" value="${fieldValue}" indexed="true">
    

    And in the form :

    private String[] field = new String[0];
    
    public final String getField(int index) {
        return field[index];
    }
    public final void setField(int index, String value) {
        //Copy last values of the array into a temporary array, then add the new value
        String[tmp] = new String[index + 1];
        for(int i=0; i<field.length; i++){
            tmp[i] = field[i];
        }
        tmp[index] = value;
        this.field = tmp;
    }