Spring2.0.4 [3]修正画面パターン
修正画面を作成。修正画面は一覧画面と同じく、画面表示時にデータをロードする処理が走り、修正ボタンを押されると更新処理が走る。
仕様
- 修正画面を開くと修正データがFormにロードされる
- 修正ボタンを押すと修正処理が走る
一覧画面修正
一覧画面に修正ボタンを追加する。
//list.jsp <c:forEach items="${userCommandList}" var="userCommand"> <tr> <td>${userCommand.userName}</td> <td> <form:form commandName="userCommandModInit" action='<%=request.getContextPath() + "/modinit.form" %>'> <input type="hidden" name="userName" value="${userCommand.userName}" /> <input type="submit" value="修正" /> </form:form> </td> </tr> </c:forEach>
修正画面作成
修正画面では修正データ初期化処理でとってきた情報をFormに設定する。
//mod.jsp<h1>修正画面</h1>
<h1>修正画面</h1>
<c:if test="${errorMsg != null}"> <div style="color:red"><c:out value="${errorMsg}" /></div> </c:if>
<form:form commandName="userCommandMod" action='<%=request.getContextPath() + "/mod.form" %>'> <table> <tr> <th>ユーザ名:</th> <td><input type="text" name="userName" value="${userCommandModInit.userName}" /></td> <td><input type="submit" value="修正" /></td> </tr> </table> </form:form>
修正データ初期化用Controller作成
修正ボタンが押されたときに、Hiddenのユーザ名からデータを取得し、修正画面で設定するためにModelAndViewに設定する。
//UserModInitController.javapublic class UserModInitController extends SimpleFormController {
private UserListLogic userListLogic;
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException bind) throws Exception {
AppLogger.debug("UserModInitController.onSubmit");
UserCommand userCommand = (UserCommand) command; String userName = userCommand.getUserName(); AppLogger.debug("userName = " + userName);
if(userName == null || "".equals(userName)){ AppLogger.debug("修正画面初期化処理失敗"); ModelAndView mav = new ModelAndView(getFormView(), bind.getModel()); mav.addObject("errorMsg", "修正画面初期化に失敗しました。"); return mav; }
UserCommand modData = this.userListLogic.getUser(userName);
if(modData == null){ AppLogger.debug("修正画面初期化処理失敗"); ModelAndView mav = new ModelAndView(getFormView(), bind.getModel()); mav.addObject("errorMsg", "修正画面初期化に失敗しました。"); return mav; }
Map map = bind.getModel(); AppLogger.debug("bind.getModel = " + map);
ModelAndView mav = new ModelAndView(getSuccessView(), bind.getModel()); mav.addObject("userName", modData.getUserName());
AppLogger.debug("修正画面初期化処理成功"); return mav; }
public UserListLogic getUserListLogic() { return userListLogic; }
public void setUserListLogic(UserListLogic userListLogic) { this.userListLogic = userListLogic; }
}
修正用ビジネスロジック作成
これは登録のときと同じ。成功したらcomplete.jspへ遷移するだけ。修正用ビジネスロジックを設定するだけでいいのはSpringならではな感じ。
Bean定義ファイル
修正画面初期化ではUserListLogicを使い、修正画面では登録画面と同じくUserControllerを使ってLogicクラスをUserModLogicに変える。
<bean id="userListLogic" class="com.daipresents.spring204.logic.UserListLogic" /> <bean id="userModLogic" class="com.daipresents.spring204.logic.UserModLogic" /> <bean name="/modinit.form" class="com.daipresents.spring204.controller.UserModInitController"> <property name="formView" value="/WEB-INF/jsp/mod.jsp" /> <property name="successView" value="/WEB-INF/jsp/mod.jsp" /> <property name="commandClass" value="com.daipresents.spring204.command.UserCommand"/> <property name="commandName" value="userCommandModInit"/> <property name="userListLogic" ref="userListLogic"/> </bean> <bean name="/mod.form" class="com.daipresents.spring204.controller.UserController"> <property name="formView" value="/WEB-INF/jsp/mod.jsp" /> <property name="successView" value="/WEB-INF/jsp/complete.jsp" /> <property name="commandClass" value="com.daipresents.spring204.command.UserCommand"/> <property name="commandName" value="userCommandMod"/> <property name="userLogic" ref="userModLogic"/> </bean>
動作確認
一覧画面には修正ボタンが表示され、それをクリックするとHiddenで「ユーザ名」が渡され、UserModInitControllerが動作して、そのユーザ名のデータを検索(ここはダミーにした)する。そして、その値を次の画面で使えるようにModelAndViewに設定。
修正画面が開くとユーザ名がFormにロードされる。修正ボタンを押したときの処理は登録のときと同じ。
課題点
Init処理(初期化処理)で取ってきたデータをFormに設定したいことはよくある。でも、今回は「mav.addObject(“userName”, modData.getUserName());」みたいなかんじに、設定して、画面で取り出して
<input type="text" name="userName" value="${userCommandModInit.userName}" />
みたいに書き出している。これはスマートではない気がするので、Init処理のControllre内で次の画面のFormに値を設定・・・とかできるきがするんだけど、やりかたは調査中。
bind.getModel()でとってくるMapがあやしいようにおもうんだけどなー。
I'm a software engineer who like travel to island in Japanese. Recently I am enjoying agile manager, coach, product owner for my work. The person grows like that.








