ActionFormに可変長の値を設定
以下のように、可変長のパラメータをActionFormのListオブジェクトに設定する方法
Jリーグの試合結果を登録する画面を例とする。
ある日付のJリーグの試合結果を全て一括登録したいという場面。
1試合分の登録内容は、ホームチーム、アウェイチーム、ホームチームスコア、アウェイチームスコア、会場の5項目。
1試合分のレコードをGameResultDtoに、レコードの集合をListに設定する。
Formクラスは以下の通り。
/** * 試合結果登録に関するFormクラス * @author @labo * */ @Component(instance = InstanceType.SESSION) public class GameResultForm implements Serializable { public List<GameResultDto> gameResultList; public List<BeanMap> studiumList; public List<BeanMap> teamList; }
入力内容をgameResultListに格納する。
GameResultDtoクラスは以下のとおり。
※必要なもののみ抜粋
public class GameResultDto implements Serializable { /** team1Idプロパティ */ private Long team1Id; /** team2Idプロパティ */ private Long team2Id; /** studiumIdプロパティ */ private Long studiumId; /** team1Scoresプロパティ */ private Integer team1Scores; /** team2Scoresプロパティ */ private Integer team2Scores; }
JSPは以下の通り。
※該当部分のみ抜粋
<table> <tr> <th>ホームチーム</th> <th>アウェイチーム</th> <th>スコア</th> <th>会場</th> </tr> <c:forEach items="${gameResultList}" var="gameResultList"> <tr> <td> <html:select name="gameResultList" property="team1Id" indexed="true"> <html:option value="0"> </html:option> <html:options collection="teamList" property="teamId" labelProperty="teamName"/> </html:select> </td> <td> <html:select name="gameResultList" property="team2Id" indexed="true"> <html:option value="0"> </html:option> <html:options collection="teamList" property="teamId" labelProperty="teamName"/> </html:select> </td> <td> <html:text name="gameResultList" property="team1Scores" size="1" indexed="true"/>−<html:text name="gameResultList" property="team2Scores" size="1" indexed="true"/> </td> <td> <html:select name="gameResultList" property="studiumId" indexed="true"> <html:option value="0"> </html:option> <html:options collection="studiumList" property="studiumId" labelProperty="studiumNameAbb"/> </html:select> </td> </tr> </c:forEach> </table>
gameResultListをループさせて出力するために、
を用いている。
GameResultFormクラスのコレクション型のプロパティgameResultListをgameResultListという名称で定義している。
ここで、varはFormクラスのプロパティ名にしないといけないため、
itemsもvarも同じ名称にする必要があるだろう。
更に
のように、
上記c:forEachで定義したgameResultListをnameとして、
GameResultDtoクラスのプロパティ名を定義することDTOクラスとのマッピングを図る。
indexed="true"が重要で、これをつけとかないとListに値が入ってこない。