// commandクラス
class ProfileCommand{
....中略....
ArrayList<Hobby> hobby;
public ArrayList<Hobby> getHobby() {
return hobby;
}
public void setHobby(ArrayList<Hobby> hobby) {
this.hobby = hobby;
}
....中略....
}
// Hobbyクラス
class Hobby{
....中略....
String type;
String comment;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
....中略....
}
// Controller
public class ProfileWizard extends AbstractWizardFormController {
....中略....
protected Object formBackingObject(HttpServletRequest request){
ProfileCommand form = new ProfileCommand();
form.setHobby(new ArrayList<Hobby>());
return form;
}
protected void onBind(HttpServletRequest request,
Object command,
BindException exception) throws Exception {
int current_page = this.getCurrentPage(request);
int target_page = this.getTargetPage(request, current_page);
if(current_page == 2 && target_page == 2){
ProfileCommand form = (ProfileCommand)command;
String type = request.getParameter("tmp_type");
String comment = request.getParameter("tmp_comment");
if(!type.equals("") && !comment.equals("")){
Hobby hobby = new Hobby();
hobby.setType(type);
hobby.setComment(comment);
form.getHobby().add(hobby);
}else{
exception.rejectValue("hobby", "", "error");
}
}
}
....中略....
}
// 3ページ目のjsp(<form>のみ)
<form method="post">
<spring:bind path="form.hobby">
type : <input type="text" name="tmp_type" value="" /><br/>
comment : <input type="text" name="tmp_comment" value="" /><br/>
<input type="submit" name="_target2" value="追加" /><br/><br/>
<div style="color:#ff0000">${status.errorMessage}</div>
</spring:bind>
<ol>
<c:forEach items="${form.hobby}" var="item">
<li>
type : ${item.type}<br />
comment : ${item.comment}<br />
</li>
</c:forEach>
</ol>
<input type="submit" name="_target3" value="次へ" /><br />
<input type="submit" name="_target1" value="戻る" />
</form>
|