728x90
원본 영상 재생목록 링크:
https://www.youtube.com/watch?v=Y7S1xXsKy_w&list=PLZzruF3-_clsWF2aULPsUPomgolJ-idGJ&index=8
게시글 리스트
controller > BoardController.java
package com.example.demo.contoller;
import com.example.demo.entity.testboard;
import com.example.demo.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/*shift + f10 재실행*/
@Controller
public class BoardContoller {
@Autowired
private BoardService boardService;
@GetMapping("/board/write") //어떤 url로 접근할 것인지 정해주는 어노테이션 //localhost:8080/board/write
public String boardWriteForm() {
return "boardwrite";
}
@PostMapping("/board/writepro")
public String boardWritePro(testboard board){
boardService.write(board);
return "";
}
//1번
@GetMapping("/board/list")
public String boardList(Model model){
return "boardList";
}
}
1번 입력
templates > boardList.html
<!--//2번-->
<!DOCTYPE html>
<html lang="en" xmlns:th="//www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>게시물 리스트 페이지</title>
</head>
<style>
.layout{
width : 500px;
margin : 0 auto;
margin-top: 40px;
} /*layout 하위에 있는 input태그 스타일 설정*/
</style>
<body>
<div class="layout">
<table>
<thead>
<tr>
<th>글번호</th>
<th>제목</th>
</tr>
</thead>
<tbody>
</tbody>
</tr>
<td>0</td>
<td>제목입니다</td>
</td>
</tbody>
</table>
</div>
</body>
</html>
2번 입력 (/board/list)
Service > BoardService.java
package com.example.demo.service;
import com.example.demo.entity.testboard;
import com.example.demo.repository.BoardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BoardService {
@Autowired //new를 써야하지만, 스프링부트가 알아서 읽어와서 주입을해준다.
private BoardRepository boardRepository;
public void write(testboard board){
boardRepository.save(board);
}
//3번
//ctrl+space = 자동 import
public List<testboard> boardList(){
//findAll : 테스트보드라는 클래스가 담긴 List를 반환하는것을 확인할수있다
return boardRepository.findAll();
}
}
3번입력
ctrl+space : List 자동 import
findAll() : 테스트 보드라는 클래스가 담긴 List를 반환하는 것을 확인할수있다
Service > BoardService.java
package com.example.demo.contoller;
import com.example.demo.entity.testboard;
import com.example.demo.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/*shift + f10 재실행*/
@Controller
public class BoardContoller {
@Autowired
private BoardService boardService;
@GetMapping("/board/write") //어떤 url로 접근할 것인지 정해주는 어노테이션 //localhost:8080/board/write
public String boardWriteForm() {
return "boardwrite";
}
@PostMapping("/board/writepro")
public String boardWritePro(testboard board){
boardService.write(board);
return "";
}
//1번
@GetMapping("/board/list")
public String boardList(Model model){
//BoardService에서 만들어준 boardList가 반환되는데, list라는 이름으로 받아서 넘기겠다는 뜻
model.addAttribute("list" , boardService.boardList()); //4번
return "boardList";
}
}
4번 입력
model.addAttribute("list" , boardService.boardList());
BoardService에서 만들어준 boardList가 반환되는데, list라는 이름으로 받아서 넘기겠다는 뜻
templates > boardList.html
<!--//2번-->
<!DOCTYPE html>
<html lang="en" xmlns:th="//www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>게시물 리스트 페이지</title>
</head>
<style>
.layout{
width : 500px;
margin : 0 auto;
margin-top: 40px;
} /*layout 하위에 있는 input태그 스타일 설정*/
</style>
<body>
<div class="layout">
<table>
<thead>
<tr>
<th>글번호</th>
<th>제목</th>
</tr>
</thead>
<tbody>
<!--5번-->
<!--list에 testboard가 담겨있는것을 하나씩 빼준다-->
<tr th:each="testboard : ${list}">
<td th:text="${testboard.id}"></td>
<td th:text="${testboard.title}"></td>
</tr>
<td>0</td>
<td>제목입니다</td>
</td>
</tbody>
</table>
</div>
</body>
</html>
5번 입력
each문을 통해 list에 testboard가 담겨있는것을 하나씩 빼준다
<tr th:each="testboard : ${list}">
<td th:text="${testboard.id}"></td>
<td th:text="${testboard.title}"></td>
728x90
'STUDY > SpringBoot' 카테고리의 다른 글
[Springboot] 게시판따라하기(4) - 게시글 삭제·수정 ✔정리 (0) | 2022.08.09 |
---|---|
[Springboot] 게시판따라하기(3) - 게시글 상세페이지 ✔정리 (0) | 2022.08.09 |
[Springboot] 게시판따라하기(1) - 게시글 작성폼생성·작성처리 ✔정리 (0) | 2022.08.09 |
[SpringBoot] IntelliJ + OracleDB 게시판 생성 ✔정리(2) (0) | 2022.08.08 |
[SpringBoot] IntelliJ + OracleDB 게시판 생성 ✔정리(1) (0) | 2022.08.07 |