クライアントからの要求をAP層に引き渡すコントローラを作成します。また、業務アプリケーションの処理結果などをビューとして表示するためのJSPを作成します。
業務アプリケーションを呼び出すコントローラの作成例を以下に示します。
■ViewEmployeeListController.java(社員一覧を表示するコントローラ)
package test; ... import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ViewEmployeeListController extends AbstractController{ @Autowired private ListService businessService; @RequestMapping("/ViewEmployeeList") public Map viewEmployeeList() { Map<String,Object> model = new HashMap<String,Object>(); // 業務アプリケーションから社員の名前一覧を取得します String[] employeeList = businessService.getEmployeeList(); // 処理結果をビューに伝播するためmodelに設定します model.put("employeeList", employeeList); return model; } } |
■ViewEmployeeList.jsp(社員一覧を表示するビュー)
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>社員名簿</title> </head> <body> <table border="1" cellspacing="2" cellpadding="3"> <tr> <td><b>No.</b></td> <td><b>名前</b></td> </tr> <!-- (1) --> <c:forEach var="employee" items="${employeeList}" varStatus="no"> <tr> <td><b></b><c:out value="${no.index + 1}"/></td> <td><c:out value="${employee}"/></td> </tr> </c:forEach> </table> </body> </html> |
(1) ModelAndViewに設定された社員全員の名前のリスト“employeeList”の情報を表示します。