Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

게으른 완벽주의자의 개발자 도전기

[board] 게시글 목록 본문

Spring Boot

[board] 게시글 목록

머리방울 2022. 9. 19. 20:37

1. mapper/ service

<mapper namespace="boardMapper">

<resultMap type="kh.study.board.board.vo.BoardVO" id="board">
<id column="BOARD_NUM" property="boardNum"/>
<result column="TITLE" property="title"/>
<result column="CONTENT" property="content"/>
<result column="MEMBER_ID" property="memberId"/>
<result column="CREATE_DATE" property="createDate"/>

</resultMap>

<select id="boardList" resultMap="board">
SELECT BOARD_NUM
	, TITLE
	, MEMBER_ID
	, TO_CHAR(CREATE_DATE, 'YYYY-MM-DD') AS CREATE_DATE
FROM BOARD
ORDER BY BOARD_NUM DESC
</select>


게시글 목록
@Override
public List<BoardVO> boardList(BoardVO board) {
		
	return sqlSession.selectList("boardMapper.boardList", board);
}

2. controller

	게시글 목록
	@GetMapping("/list")
	public String boardList(BoardVO boardVO, Model model) {
		
		model.addAttribute("boardList", boardService.boardList(boardVO));
	
		return "content/board_list";
	}

3. board_list.html

<div layout:fragment ="content">

	<table class="table table-striped table-hover">

		<thead>
			<tr>
				<th scope="col">글 번호</th>
				<th scope="col">제목</th>
				<th scope="col">작성자</th>
				<th scope="col">작성일</th>
			</tr>
		</thead>
		<tbody>
		   <th:block th:if="${#lists.size(boardList) == 0}">
            <tr>
				<th colspan="4">게시글이 없습니다.</th>
			</tr>
           </th:block>
		</tbody>

		<tbody>
			<th:block th:unless="${#lists.size(boardList) == 0}"
				th:each="board : ${boardList}">
				<tr>
					<th scope="row" th:text="${board.boardNum}"></th>
					<td><a
						th:href="@{/board/boardDetail(boardNum=${board.boardNum})}"
						th:text="${board.title}"></a></td>
					<td th:text="${board.memberId}"></td>
					<td th:text="${board.createDate}"></td>
				</tr>
			</th:block>
		</tbody>

	</table>

	로그인 한 유저만 글쓰기 버튼 보이도록 설정
	<div class="row" text-center>
	
	<th:block th:if="${session.loginInfo != null}">
		<button type="button" class="btn btn-outline-info" th:onclick="|location.href='@{/board/writeBoard}'|">글쓰기</button>	
	</th:block>
	</div>
</div>

기존 jsp에서 size는 boardList.size였지만 타임리프에서는 th:if="${#lists.size(boardList) == 0} 작성한다.