クライアントからの要求をAP層に引き渡すコントローラを作成します。また、業務アプリケーションの処理結果などをビューとして表示するためのJSPを作成します。
業務アプリケーションを呼び出すコントローラの作成例を以下に示します。
■ViewEmployeeListController.java(社員一覧を表示するコントローラ)
package test; ... import org.springframework.context.ApplicationContext; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class ViewEmployeeListController extends AbstractController{ public ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception { Map model = new HashMap(); // Beanを取得します ApplicationContext context = getWebApplicationContext(); ListService employee =(ListService)context.getBean("businessService"); // 業務アプリケーションから社員の名前一覧を取得します String[] EmployeeList = null; EmployeeList = employee.getEmployeeList(); // 処理結果をビューに伝播するためmodelに設定します model.put("employeeList", EmployeeList); // ビューで表示するためにModelAndViewオブジェクトを作成し、処理結果を格納します ModelAndView mav = new ModelAndView("ViewEmployeeList", model); return mav; } } |
■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”の情報を表示します。