사용자 데이터를 표시하는 index.jsp 파일을 작성합니다.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<html>
<head>
<style>
table, td, th {
border : 1px solid black;
border-collapse : collapse;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Boot Application</title>
</head>
<body>
<h2>Sample User List</h2>
<table>
<thead>
<tr>
<th>USER ID</th>
<th>NAME</th>
<th>NUMBER</th>
</tr>
</thead>
<c:forEach var="item" items="${userList}" varStatus="idx">
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.number}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
해당 파일은 application.ym의 설정에 의해 다음과 같은 속성을 가집니다.
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
userList.jsp 페이지를 호출하는 간단한 controller를 작성합니다. 해당 REST API는 GET방식으로 호출되며, URL은 http://{SERVER_PORT}/${SERVER_CONTEXT}/api/basic/sample/userList 입니다.
@GetMapping(value = "/api/basic/sample/userList")
public ModelAndView getUserList(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("userList", sampleService.getUserList());
modelAndView.setViewName("userList");
return modelAndView;
}
line 4 - userList라는 이름으로 서비스로부터 받아온 사용자 목록 데이터를 modelAndView 객체에 추가하며, 뷰에서 userList 데이터를 사용할 수 있습니다. line 5 - 뷰 이름을 index로 설정하며, userList.jsp 파일을 의미합니다. line 6 - 설정된 ModelAndView 객체를 반환합니다.