フォームに複数のテキストボックスがあって、その値をモデルのリストを使って表現する方法です。
例えば、、、こんな画面とか。

うーん。どこで使うんやろ・・・。実は用途無しか?(ダメ)

ソース


HTML


HTML側は、Formにテキストボックスを必要な数だけ用意します。
wicket:id を、”名称[インデックス]”の形式にします。
インデックス部分が、Listのインデックスと対応します。


テキストボックスを任意の個数にする方法がわからないので、必要な最大数用意しています。
任意個の配置方法は、調査課題かな・・・

<form wicket:id="form" >
  <input type="text" wicket:id="friendNames[0]" /><br />
  <input type="text" wicket:id="friendNames[1]" /><br />
  <input type="text" wicket:id="friendNames[2]" /><br />
  <input type="text" wicket:id="friendNames[3]" /><br />
  <input type="text" wicket:id="friendNames[4]" /><br />
  <input type="text" wicket:id="friendNames[5]" /><br />
  <input type="text" wicket:id="friendNames[6]" /><br />
  <input type="text" wicket:id="friendNames[7]" /><br />
  <input type="text" wicket:id="friendNames[8]" /><br />
  <input type="text" wicket:id="friendNames[9]" /><br />
  <input type="submit" wicket:id="submit" />
</form>

Java


Java側は、以下の3ステップです。
1.用意した数だけTextFieldをフォームに配置。
2.wicket:id の名前の部分と同じプロパティを持ったモデルを用意(ここでは、MekeModelクラスを用意
3.CompoundPropertyModelを使ってモデルをフォームに紐付け


1.3.の部分のコードは以下
private MekeModel mekeModel = new MekeModel();

public MekePage(){
	initializedComponent();
}

protected void initializedComponent(){
	Form form = new Form("form");
	this.add(form);
	
	form.setModel(new CompoundPropertyModel(mekeModel));
	
	form.add(new TextField("friendNames[0]"));
	form.add(new TextField("friendNames[1]"));
	form.add(new TextField("friendNames[2]"));
	form.add(new TextField("friendNames[3]"));
	form.add(new TextField("friendNames[4]"));
	form.add(new TextField("friendNames[5]"));
	form.add(new TextField("friendNames[6]"));
	form.add(new TextField("friendNames[7]"));
	form.add(new TextField("friendNames[8]"));
	form.add(new TextField("friendNames[9]"));

2.の部分のコードは以下
private static class MekeModel implements IClusterable
{
	private List list = new LinkedList();
	
	public MekeModel(){
		list.add("めけ0");
		list.add("めけ1");
		list.add("めけ2");
		list.add("めけ3");
		list.add("めけ4");
		list.add("めけ5");
		list.add("めけ6");
		list.add("めけ7");
		list.add("めけ8");
		list.add("めけ9");
	}
	
	public List getInvitedAddress(){
		return list;
	}
}

これで、MekeModelのListの値が初期値として使われ、ポストバックされると入力された値がセットされるようになります。
リストのアイテム数がテキストフィールドの数より少ない場合、IndexOutOfBoundsException が発生するので注意が必要です。

タグ:

java wicket
最終更新:2007年10月10日 18:10