각 게시판의 board_info_idx를 통해 board_info_name을 가져오는 작업을 하겠다.
@Select("select board_info_name " +
"from board_info_table " +
"where board_info_idx = #{board_info_idx)")
String getBoardName(int board_info_idx);
boardDao, boardService에서도 작업을 한 후 boardController에서 boardService를통해 이름을 가져오자.
@GetMapping("/board/main")
public String main(@RequestParam("board_info_idx") int board_info_idx, Model model) {
model.addAttribute("board_info_idx",board_info_idx);
String board_info_name = boardService.getBoardName(board_info_idx);
model.addAttribute("board_info_name",board_info_name);
return "board/main";
}
board의 main.jsp에서 board_info_name을 출력한다.
<h4 class="card-title">${board_info_name }</h4>
아래와 같이 무슨 게시판인지가 잘 나타난다.

글 목록을 구현하겠다. 아직 글을 저장하는 것 까지는 구현했지만 목록은 임시적인 글들만 보이고 있다.
글 목록 main 페이지에 보여줄 내용은 글 번호, 제목, 작성자, 작성날짜이다.
content table의 content_board_idx와 board_info_idx가 같으며, content table의 content_writer_idx와 user table의 user_idx가 같은 조건에서 위의 네가지 정보를 가져오자.
mapper는 아래와 같다. 이때 날짜는 Mybatis양식에 맞게 to_char로 가져오면 된다. 그리고 다른 테이블의 내용이나 날짜는 가져올때 이름이 없는데 as를 통해 이름을 부여해주자.
@Select("select C.content_idx, C.content_subject, U.user_nickname as content_writer_name, " +
"to_char(C.content_date, 'YYYY-MM-DD') as content_date " +
"from content_table C, user_table U " +
"where C.content_writer_idx = U.user_idx and C.content_board_idx = #{board_info_idx} " +
"order by C.content_idx desc")
List<ContentBean> getContentList(int board_info_idx);
가져온 글의 인덱스, 제목, 유저 닉네임, 작성일을 ContentBean에 담아서 List로 반환한다.
그러기 위해서는 ContentBean에 저 네가지 필드가 모두 멤버 변수로 있어야 하므로 ContentBean에 멤버를 추가로 선언하자.
private String content_date;
private String content_writer_name;
이제 boardDao, boardService를 작성하고 board controller에서 호출 후 model로 전달하자.
List<ContentBean> list = boardService.getContentList(board_info_idx);
model.addAttribute("contentList", list);
위 코드를 추가한다.
이제 board/main.jsp에서 전달받은 정보를 출력하자. 전달받은 list를 obj라는 이름으로 받아와서 각각 출력한다.
<tbody>
<c:forEach var='obj' items="${contentList }">
<tr>
<td class="text-center d-none d-md-table-cell">${obj.content_idx }</td>
<td><a href='${root }board/read'>${obj.content_subject }</a></td>
<td class="text-center d-none d-md-table-cell">${obj.content_writer_name }</td>
<td class="text-center d-none d-md-table-cell">${obj.content_date }</td>
</tr>
</c:forEach>
</tbody>

이렇게 글 정보가 잘 나오는 것을 확인할 수 있다.
'프로젝트 > 게시판미니프로젝트' 카테고리의 다른 글
[프로젝트] 글 수정 - 작성 권한 처리 (0) | 2020.05.12 |
---|---|
[프로젝트] 게시판 글 읽는 페이지 구현 (0) | 2020.05.12 |
[프로젝트] 게시글 작성하기 (0) | 2020.05.11 |
[프로젝트] 회원 정보 수정 (0) | 2020.05.08 |
[프로젝트] 상단 메뉴 처리, 및 로그아웃 (0) | 2020.05.08 |