JSPの修正
では、リストを表示するJSPを作成しましょう。今回も、前回のサンプルを修正する形で利用することにします。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="stripes"
uri="http://stripes.sourceforge.net/stripes.tld"%>
<jsp:useBean id="optionmodel" scope="page"
class="jp.codezine.OptionsModel"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8">
<title>Helo</title>
</head>
<body>
<h1>Helo Stripes.</h1>
<p>コントロールサンプル</p>
<stripes:form beanclass="jp.codezine.HeloActionBean">
<table>
<tr>
<td>Result:</td>
<td>${actionBean.result}</td>
</tr>
<tr>
<td>Select:</td>
<td>
<stripes:select name="sel">
<stripes:options-collection collection="${optionmodel.options}"
label="label" value="value" />
</stripes:select>
</td>
</tr>
<tr>
<td colspan="2">
<stripes:submit name="submit" value="Submit" />
</td>
</tr>
</table>
</stripes:form>
</body>
</html>
まず最初に、<jsp:useBean>を使い、id="optionmodel" scope="page" class="jp.codezine.OptionsModel"という形でBeanオブジェクトを用意しています。そして、これを利用してリストを生成しています。ここでの<stripes:select>部分を見てみると、次のようになっています。
<stripes:select name="sel">
<stripes:options-collection collection="${optionmodel.options}"
label="label" value="value" />
</stripes:select>
<stripes:options-collection>というタグが内部に組み込まれています。これは、collection属性に設定されたコレクションからオブジェクトを取得し、それをもとに<option>タグを生成するものです。labelとvalueという2つの属性が用意されており、これらにそれぞれプロパティ名が記述されています。整理すると、次のようになります。
<stripes:options-collection collection="コレクション"
label="プロパティ名" value="プロパティ名" />
これにより、Stripesはcollectionのコレクションからオブジェクトを取得し、そこからlabelとvalueに指定されたプロパティの値を取り出して<option>タグを書き出していくのです。
このようなリスト項目を生成するタグには、この他に<stripes:options-map>、<stripes:options-enumeration>といったものが用意されています。それぞれ、MapやEnumから順に値を取得して表示していくもので、基本的な使い方は<stripes:options-collection>とほぼ同じです。
<stripes:select>の値を取得する
では、作成したJSPのフォームを送信した時の簡単な処理をあげておきましょう。選択されたリスト項目を表示するだけの簡単なものです。
package jp.codezine;
import java.util.*;
import net.sourceforge.stripes.action.*;
public class HeloActionBean implements ActionBean {
private ActionBeanContext context;
private String result;
private String sel;
public String getSel() { return sel; }
public void setSel(String sel) { this.sel = sel; }
……中略……
@DefaultHandler
public Resolution submit() {
this.result = "あなたの入力した値:" + sel;
return new ForwardResolution("/helo.jsp");
}
}

ActionBean側に用意すべきは、<stripes:select>の値を保管するプロパティのみです。<stripes:options-collection>は<option>タグを生成するものなので、これ自身は何ら値は返しません。
