SimpleFormControllerを使うとFormを使った処理が簡単にできる。WebアプリケーションではこのTypeの処理をよくするので。
設定項目
以下はBaseCommandControllerから受け継いでいる。
- commandName
- RequestにCommandクラスをバインディングするときに使う名前。
- commandClass
- リクエストを受け取るときやリクエストパラメタを受け取るときに使うクラスを指定する。
- formView
- ユーザが新しいFormを使うとき(はじめて画面を表示するとき)や妥当性エラーが発生したときのViewを指定する
- successView
- 処理に成功したときのViewを指定する。
実装例
public class RegController extends SimpleFormController {
private RegLogic regLogic;
public RegLogic getRegLogic() {
return regLogic;
}
public void setRegLogic(RegLogic regLogic) {
this.regLogic = regLogic;
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException bind)
throws Exception {
System.out.println("RegController.onSubmit");
//CommandからFormの値を取得
RegCommand regCommand = (RegCommand) command;
System.out.println("UserName = " + regCommand.getUserName());
//BusinessLogic実行
this.regLogic.execute(regCommand);
//次の画面へ
ModelAndView mav = new ModelAndView(getSuccessView(), bind.getModel());
mav.addObject("successMsg", "登録に成功しました。");
return mav;
}
}
Bean定義ファイル例
DispatcherServlet-servlet.xmlに記述。
<bean name="/reg.form"
class="com.daipresents.spring204.controller.RegController">
<!-- GETの時の遷移先 -->
<property name="formView" value="/WEB-INF/jsp/reg.jsp" />
<!-- 成功時の遷移先 -->
<property name="successView" value="="/WEB-INF/jsp/reg.jsp" />
<property name="commandClass" value="com.daipresents.spring204.command.RegCommand"/>
<property name="commandName" value="regCommand"/>
<property name="regLogic" ref="regLogic"/>
</bean>
<bean id="regLogic" class="com.daipresents.spring204.logic.RegLogic">
</bean>