POJO FormでのIndexって
POJO FormのIndexの扱いについていろいろありそうなので調査。
まず、自分がよく使うDTOの親子関係から。
DTOはこんな感じにして
public class ParentDto { private List children = new ArrayList(); public ChildDto getChild(int index) { while (children.size() < index + 1) { children.add(new ChildDto()); } return (ChildDto) children.get(index); } } public class ChildDto { private String text; public String getText() { return text; } public void setText(String text) { this.text = text; } }
JSPでは
<html:text property="child[0].text"/>
こんな感じで入力して
<bean:write name="parentDto" property="child[0].text"/>
こんな感じで出力。
うまく動いた。ほっ
こっからは未知の世界(Strutsで使ったことがない。。。)
DTOにsetHoge(int index, String value)を持った場合
DTO
public class ArrayDto { private List values = new ArrayList(); public String getValue(int index) { while (values.size() < index + 1) { values.add(""); } return (String) values.get(index); } public void setValue(int index, String value) { while (values.size() < index + 1) { values.add(""); } values.remove(index); values.add(index, value); } }
<html:text property="value[0]"/>
<bean:write name="arrayDto" property="value[0]"/>
入力画面を表示するところはOKだけど、プロパティをセットするところでjava.lang.NullPointerException。。。うぅ
public class ArrayDto { private String[] names = new String[1]; public String[] getNames() { return names; } public void setNames(String[] names) { this.names = names; } }
<html:text property="names[0]"/>
<bean:write name="arrayDto" property="names[0]"/>
これはうまくいった。よかった。。。けど、固定長の配列ってあまりつかわなそう。。。
DynaActionFormについてちょっと見てみよー。
あとMapも試さないといけないなー。
[追記]
Mapは問題なく動作した。
setHoge(int index, String value)で動作しなかったのはindex指定なしのプロパティアクセスを定義してなかったから。
String[] getHoge()を定義したら問題なく動作。知識不足だね。。。はずかしい。
でも、よかった。。。