明細一覧に入力フィールドを作成し、同じ項目を複数登録する時、ActionFormのArrayListにIOできると便利。
ArrayListの要素は自作のClassのインスタンスが使用可能。
[Java]
public class Detail{
String detailField1="abcdefg" ;
//getter & setter ・・・
}
public class MyAction(...){
...
ArrayList al = new ArrayList();
al.add(new Detail()); //必要な数だけ繰り返す
al.add(new Detail());
al.add(new Detail());
myForm.set("detail", al);
}
[JSP]
<logic:iterate id="detail"
name="myForm"property="detail" >
<html:text name="detail" property="detailField1" indexed="true"/>
</logic:iterate>
[出力]
<input type="text" name="detail[0].detailField1" value="abcdefg" />
<input type="text" name="detail[1].detailField1" value="abcdefg" />
<input type="text" name="detail[2].detailField1" value="abcdefg" />
[おまけのJS]
function getListObject(list,
field,index){
return document.forms[0].elements[list+"[" + index + "]." + field ];
}
注意点
・logic:iterateのid(html:textのnameでもある)とActionFormのListの変数を同じ名前にする。
・indexedをtrueにする。
・あらかじめリストに必要な数だけ要素を追加しておく
参考
http://civic.xrea.jp/2006/06/09/struts-using-list/