一覧画面を作成してみる。一覧画面は、表示するときにデータ取得処理が走る。
登録画面作成で気が付いた点を設計に反映しているので、構造がかなり変わっているから注意。
仕様
- メニューの一覧画面リンクをクリックすると一覧画面に遷移する
- 一覧画面にはユーザ名の一覧が表示される
- 一覧画面には登録ボタンがある
- 登録ボタンをクリックすると登録画面に遷移する
- 一覧画面には修正ボタンがある
- 修正ボタンをクリックすると対象のユーザの修正画面へ遷移する
メニューの作成
一覧画面へのリンクを作成するだけ
//menu.jsp
<ul>
<li><a href="<%= request.getContextPath() %>/list.form">一覧画面</a></li>
</ul>
一覧画面の作成
一覧画面には登録画面、修正画面へのリンクがある。
//list.jsp
<h1>一覧画面</h1>
<a href="<%= request.getContextPath() %>/reg.form">登録画面</a>
<c:choose>
<c:when test="${userCommandList != null}">
<table border="1">
<tr>
<th>ユーザ名</th>
<th> </th>
</tr>
<c:forEach items="${userCommandList}" var="userCommand">
<tr>
<td>${userCommand.userName}</td>
<td><a href="<%= request.getContextPath() %>/mod.form">修正</a></td>
</tr>
</c:forEach>
</table>
</c:when>
<c:otherwise>
データがありません。
</c:otherwise>
UserListControllerの作成
UserListControllerによって、一覧画面で表示するデータを取得する。
//UserListController.java
public class UserListController extends AbstractController {
private UserListLogic userListLogic;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
AppLogger.debug("一覧を取得");
List<UserCommand> userCommandList = userListLogic.getAllUser();
ModelAndView mav = new ModelAndView("/WEB-INF/jsp/list.jsp");
if(userCommandList != null && !userCommandList.isEmpty()){
AppLogger.debug("userCommandListが取得できた");
mav.addObject("userCommandList", userCommandList);
}else{
AppLogger.debug("userCommandListが取得できなかった");
mav.addObject("userCommandList", null);
}
return mav;
}
public UserListLogic getUserListLogic() {
return userListLogic;
}
public void setUserListLogic(UserListLogic userListLogic) {
this.userListLogic = userListLogic;
}
}
ビジネスロジッククラス作成
一覧画面のデータを取ってくるビジネスロジックを作成。ダミーで10件のデータを作成する実装にしている。
//UserListLogic.java
public class UserListLogic {
public List<UserCommand> getAllUser() {
AppLogger.debug("UserListLogic.getAllUser");
List<UserCommand> userCommandList = new ArrayList<UserCommand>();
for (int cnt = 0; cnt < 10; cnt++) {
UserCommand userCommand = new UserCommand();
userCommand.setUserName("UserName" + cnt);
userCommandList.add(userCommand);
}
return userCommandList;
}
}
Bean定義ファイル作成
DispatcherServlet-servlet.xmlに以下を記述。
//DispatcherServlet-servlet.xml
<bean id="userListLogic" class="com.daipresents.spring204.logic.UserListLogic" />
<bean name="/list.form"
class="com.daipresents.spring204.controller.UserListController">
<property name="userListLogic" ref="userListLogic"/>
</bean>