Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 200
Marouane Gazanayi Avatar asked Nov 05 '22 10:11

Marouane Gazanayi


2 Answers

According to my knowledge,
1. If it is struts 1, the dollar "$" field does not work to take the values. 2. You should not specify the index in the property name, but it will be automatically used by the tag translator and hence your code will something 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">  

I hope this helps you to solve your problem.

like image 53
Naved Avatar answered Nov 09 '22 18:11

Naved


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;
}
like image 31
Marouane Gazanayi Avatar answered Nov 09 '22 16:11

Marouane Gazanayi