Struts 1: Put jsp values ​​in a form that uses a Java list

In my jsp, I have 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 java.util.list, which I need to fill out from the fields above:

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

+5
source share
2 answers

Just do it

<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;
}
+1
source

According to my information,
1. If this is position 1, the dollar field "$" does not work to take values. 2. You do not have to specify an index in the property name, but it will be automatically used by the tag translator, and therefore your code will look like

 <html:text property="field" indexed="true"> 
 <html:text property="field" indexed="true">
 <html:text property="field" indexed="true">
 <html:text property="field" indexed="true">  

, .

+1

All Articles