Struts 1: положить значения JSP в форму, которая использует список Java - PullRequest
5 голосов
/ 05 декабря 2011

В моем jsp у меня есть несколько полей вроде этого:

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

И в моей форме у меня есть java.util.list, который мне нужно заполнить из полей сверху:

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

Проблема в том, что список не заполнен.Есть идеи ???Спасибо !!

Ответы [ 2 ]

1 голос
/ 05 декабря 2011

Просто сделайте это

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

И в виде:

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 голос
/ 05 декабря 2011

По моим сведениям,
1. Если это число 1, поле доллара «$» не работает для получения значений. 2. Не следует указывать индекс в имени свойства, но он будет автоматически использоваться переводчиком тегов, и, следовательно, ваш код будет выглядеть примерно так:

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

Надеюсь, это поможет вам решить вашу проблему.

...