습관제작소
22-09-05 JavaSpringBoot BoardList, Content 생성 본문
SMHRD or ITSchool/JavaSpring and Boot
22-09-05 JavaSpringBoot BoardList, Content 생성
KUDO 2022. 9. 6. 09:11BoardList
- SpringBoot01Application.java
package com.smhrd.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.smhrd.controller") // 해당 패키지를 스캔하여 Comtroller를 bean으로 등록
@SpringBootApplication
public class SpringBoot01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01Application.class, args);
}
}
- BoardController.java
package com.smhrd.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.smhrd.domain.Board;
@Controller
public class BoardController {
@RequestMapping("/boardList.do")
public String boardList(Model model) {
List<Board>list = new ArrayList<Board>();
list.add(new Board(1, "스프링 게시판1", "사용자1", "스프링 부트1", "22/09/05", 0));
list.add(new Board(2, "스프링 게시판2", "사용자2", "스프링 부트2", "22/09/05", 0));
model.addAttribute("list",list);
return "view/boardList"; // 기준은 탬플릿 templates/index.html
// templates/view/boradLsit
}
}
- domain > Board.java
package com.smhrd.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Board {
private int idx;
private String title;
private String writer;
private String content;
private String indate;
private int count;
}
- boardList.html.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>게시글 목록</h1>
<!--
JSTL + EL(JSP)
<c:forEach var="board" item="${list}">
<tr>
<td>${board.title}</td>
</tr>
</c:forEach>
>> 거의 모든 로직을 jsp 내부에서 구현가능
>> 비지니스로직을 JSP에서 최대한 안쓰려고 노력
>> JSP > (JSTL과 EL 동작) > Servlet > class > HTML > HTML파싱
>> taglib 지시자를 이용해서 선언
HTML??
thymeleaf(HTML) : HTML에서 서버내 데이터를 출력, 조작
>> 대체로 출력에 관한 로직만 구현 가능
>> HTML 파싱될때 실행
>> html 태그에 xmlns:th 속성을 추가하여, 적용
>> 속성값을 사용하여 적용
-->
<table border="1">
<tr>
<th>글번호</th>
<th>제목</th>
<th>작성자</th>
<th>작성일</th>
<th>조회수</th>
</tr>
<!-- th:each="변수명 : 배열" -->
<tr th:each="board : ${list}">
<!-- th:text="${변수명} 태그 값을 집어넣을 때 사용" -->
<th th:text="${board.idx}">글번호</th>
<th><a th:text="${board.title}" href="boardContent.do">제목</a></th>
<th th:text="${board.writer}">작성자</th>
<th th:text="${board.indate}">작성일</th>
<th th:text="${board.count}">조회수</th>
</tr>
</table>
</body>
</html>
BoardContent
- BoardController.java
package com.smhrd.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.smhrd.domain.Board;
@Controller
public class BoardController {
@RequestMapping("/boardList.do")
public String boardList(Model model) {
List<Board>list = new ArrayList<Board>();
list.add(new Board(1, "스프링 게시판1", "사용자1", "스프링 부트1", "22/09/05", 0));
list.add(new Board(2, "스프링 게시판2", "사용자2", "스프링 부트2", "22/09/05", 0));
model.addAttribute("list",list);
return "view/boardList"; // 기준은 탬플릿 templates/index.html
// templates/view/boradLsit
}
@RequestMapping("/boardContent.do")
public String boardContent(Model model) {
Board board = new Board(1,"스프링 게시판","백성연","SpringBoot 게시판","22/09/05",0);
model.addAttribute("board",board);
return "view/boardContent";
}
}
- boardContent.html
<table border="1">
<tr>
<!-- th:text="${변수명} 태그 값을 집어넣을 때 사용" -->
<th th:text="${board.idx}">글번호</th>
<th th:text="${board.title}" >제목</th>
<th>작성자 : [[""${board.writer}"]]</th>
<!--
우리가 원하는 곳에 값을 집어넣을 때는
[[....]] >> 사용
-->
<th th:text="${board.content}">작성자</th>
<th th:text="${board.indate}">작성일</th>
<th th:text="${board.count}">조회수</th>
</tr>
</table>
</body>
</html>
'SMHRD or ITSchool > JavaSpring and Boot' 카테고리의 다른 글
22-09-06 JavaSpringBoot 회원가입 (0) | 2022.09.16 |
---|---|
22-09-06 JavaSpringBoot DB연결 (0) | 2022.09.06 |
22-09-05 JavaSpringBoot 환경설정(JSP, HTML 환경구축) (0) | 2022.09.05 |
22-09-02 JavaSpring Ajax 활용(게시글 삭제, 게시글 검색)/@Controller, @Restcontroller (0) | 2022.09.03 |
22-08-29 JavaSpring Ajax 활용(테이블 Insert) (0) | 2022.08.29 |
Comments