1. SampleModel.java 생성
- com.anse.web.model 패키지 생성 후 SampleModel.java 생성

2. SampleModel.java 소스 수정
-게시판 번호, 타이틀, 컨텐츠 등으로 테스트 할 예정
package com.anse.web.model;
import lombok.Builder;
import lombok.Data;
@Builder
@Data
public class SampleModel {
private int board_no;
private String title;
private String board_content;
}
3. SampleController.java 소스 수정
package com.anse.web.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import com.anse.web.model.SampleModel;
@Controller
public class SampleController {
@GetMapping("/sample")
public ModelAndView goHome(HttpServletRequest request, ModelAndView mv) {
mv.setViewName("sample/sample");
List<SampleModel> list = new ArrayList<>();
SampleModel sampleModel;
// test1
sampleModel = SampleModel.builder()
.board_no(1)
.title("제목 1")
.board_content("내용 1").build();
list.add(sampleModel);
// test2
sampleModel = SampleModel.builder()
.board_no(2)
.title("제목 2")
.board_content("내용 2").build();
list.add(sampleModel);
mv.addObject("list", list);
return mv;
}
}
4. sample.html 소스 수정
- model 객체를 담은 list를 타임리프 문법으로 loop 돌려 사용
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
<title>Sample</title>
<body>
<h2>This is Sample</h2>
<table border="1">
<tr>
<th>boardId</th>
<th>title</th>
<th>boardContent</th>
</tr>
<th:block th:each="info : ${list}">
<tr>
<td th:text="${info.board_no}"></td>
<td th:text="${info.title}"></td>
<td th:text="${info.board_content}"></td>
</tr>
</th:block>
</table>
</body>
</html>
5. 화면 확인

'MAC DEV > [mac]STS 웹 프로젝트 세팅' 카테고리의 다른 글
06. mybatis 연동, mySql 연동 (0) | 2021.04.15 |
---|---|
05. mySql 설치 및 mySql 설정 (0) | 2021.04.14 |
03. Thymeleaf(타임리프) 설정 및 기본 연동 (0) | 2021.04.12 |
02. STS4 프로젝트 생성 (Spring Starter Project) (0) | 2021.04.11 |
01. sts 설치 + lombok(롬복) 세팅 (0) | 2021.04.10 |