느림보 개발
10. SpringMVC - 게시글 목록 조회 및 상세 페이지 본문
구현할 기능 : 게시글 목록 조회 및 상세페이지
전체 흐름 : 게시글 목록 구현 후 → 게시글 상세 페이지 구현
Controller
▶ 게시글 목록 조회
- 게시글 목록페이지를 매핑한다.
- 스프링 시큐리티가 제공하는 Principal 객체를 사용해서 접속한 사용자의 아이디를 String에 저장한다.
- 저장한 아이디를 가지고 boardList를 보여줄 서비스 로직을 시행한다.
- 데이터베이스에서 가져온 결과를 Model 객체에 담아 JSP 페이지로 보낸다.
▶ 게시글 상세
- 게시글 상세페이지를 매핑한다.
- url 을 통해 게시글 번호(dno)를 파라미터로 넘겨 받는다.
- 넘겨 받은 dno를 통해 게시글 상세페이지를 보여줄 서비스 로직을 실행한다.
- 데이터베이스에서 가져온 결과를 Model 객체에 담아 jsp 페이지로 보낸다.
// 글 목록
@GetMapping("/mydiary")
public void getMydiary(Principal principal, Model model) throws Exception {
log.info("getMydiary() 호출 - 일기장 호출 ");
String userid = principal.getName();
log.info("@@사용자는 누구?@@" + userid );
List<DiaryBoardVO> boardList = service.getListD(userid);
int total = service.total(userid);
log.info("안담겻나?????"+boardList+"////// 글 개수 : "+total);
model.addAttribute("boardList", boardList);
model.addAttribute("total",total);
}
// 글 상세
@PreAuthorize("principal.username == #vo.userid")
@GetMapping("/mydiaryread")
public void getMydiaryread(@RequestParam("dno") Integer dno, Model model) throws Exception {
log.info("getMydiaryread() 호출 - 일기 글 상세");
log.info(dno+"----------------------------dno");
log.info("글 상세 정보 == "+service.readD(dno));
model.addAttribute("vo", service.readD(dno));
}
Service, Impl & DAO, Impl
▶ 게시글 목록 조회
- 사용자 아이디를 Controller → Service → DAO → mapper 로 가져가 로직을 수행한다.
- 게시글 목록의 반환 타입은 BoardVO에 저장하여 List형식으로 반환한다.
▶ 게시글 상세
- 넘겨받은 dno를 가지고 dao를 호출한다.
- 데이터베이스로부터 받은 데이터는 VO객체에 담아 반환한다.
// Service - 글 목록
public List<DiaryBoardVO> getListD(String userid) throws Exception;
// Service - 글 개수
public int total(String userid) throws Exception;
// Service - 글 상세
public DiaryBoardVO readD(Integer dno) throws Exception;
// ServiceImpl - 글 목록
@Override
public List<DiaryBoardVO> getListD(String userid) throws Exception {
List<DiaryBoardVO> boardList = dao.getListD(userid);
return boardList;
}
// ServiceImpl - 글 개수
@Override
public int total(String userid) throws Exception {
int total = dao.total(userid);
return total;
}
// ServiceImpl - 글 상세
@Override
public DiaryBoardVO readD(Integer dno) throws Exception {
DiaryBoardVO readList = dao.readD(dno);
return readList;
}
▶ 게시글 목록 조회
- 전달받은 아이디값으로 데이터베이스의 #{} 안의 값을 채워 sql 구문을 수행하도록 한다.
- 또한 select의 여러값을 가져올 것이기 때문에 selectList 메서드를 사용한다.
▶ 게시글 상세
- dno를 가지고 데이터베이스의 #{} 채워 sql 구문을 수행하도록 한다.
- 조회에 대한 결과가 행 1줄이기 때문에 selectOne 메서드를 사용한다.
// DAO - 글 목록
public List<DiaryBoardVO> getListD(String userid) throws Exception;
// DAO - 글 개수
public int total(String userid) throws Exception;
// DAO - 글 상세
public DiaryBoardVO readD(Integer dno) throws Exception;
// DAOImpl - 글 목록
@Override
public List<DiaryBoardVO> getListD(String userid) throws Exception {
log.info("getListD(String userid) -- 다이어리 목록 실행 "+ userid +"@@@@@@@");
List<DiaryBoardVO> boardList = sqlSession.selectList(NAMESPACE+".listD",userid);
return boardList;
}
// DAOImpl - 글 개수
@Override
public int total(String userid) throws Exception {
log.info("total(String userid) -- 다이어리 글 개수 실행 "+ userid +"@@@@@@@");
int total = sqlSession.selectOne(NAMESPACE+".totalD",userid);
return total;
}
// DAOImpl - 글 상세
@Override
public DiaryBoardVO readD(Integer dno) throws Exception {
return sqlSession.selectOne(NAMESPACE+".readD",dno);
}
mapper
▶ 게시글 목록 조회
- #{} 을 채울 값을 전달 받아 sql 실행한 뒤 DiaryBoardVO에 데이터를 담아 반환한다.
▶ 게시글 상세
- #{} 을 채울 값을 전달 받아 sql 실행한 뒤 DiaryBoardVO에 데이터를 담아 반환한다.
<!-- 글목록 -->
<select id="listD" resultType="com.ddd.domain.DiaryBoardVO">
select * from tbl_diary_board
where userid=#{userid}
order by dno desc;
</select>
<!-- 글 개수-->
<select id="totalD" resultType="int">
select count(*) from tbl_diary_board
where userid=#{userid}
</select>
<!--글 상세-->
<select id="readD" resultType="com.ddd.domain.DiaryBoardVO">
select * from tbl_diary_board
where dno=#{dno}
</select>
jsp - 게시글 목록
▶ 게시글 목록 조회
- jstl의 태그 라이브러리를 사용하여 forEacn 문을 돌려서 목록을 출력할 수 있게 한다.
- 페이지 상단에 라이브러리 선언 필수
▶ 게시글 상세
- 제목에 <a> 태그를 걸어 조회할 상세 페이지의 파라미터 값을 전달한다.
<table class="table table-bodered">
total : ${total }
<tr>
<th class="text-center">date</th>
<th class="text-center">title</th>
<th class="text-center">writer</th>
<th class="text-center">mood</th>
</tr>
<c:forEach var="i" items="${boardList }">
<tr>
<td class="text-center">${i.write_date }</td>
<td class="text-center"><a
href="/board/mydiaryread?dno=${i.dno }">${i.title }</a></td>
<td class="text-center">${i.userid }</td>
<c:if test="${i.score == '1'}">
<td class="text-center">♥</td>
</c:if>
<c:if test="${i.score == '2'}">
<td class="text-center">♥♥</td>
</c:if>
<c:if test="${i.score == '3'}">
<td class="text-center">♥♥♥</td>
</c:if>
<c:if test="${i.score == '4'}">
<td class="text-center">♥♥♥♥</td>
</c:if>
<c:if test="${i.score == '5'}">
<td class="text-center">♥♥♥♥♥</td>
</c:if>
</tr>
</c:forEach>
</table>
jsp - 글 상세페이지
- 글 작성 form을 복사하여 만들어준다.
- EL 표현식을 통해 데이터를 가져온다.
.. 생략 ..
<div class="form-group">
<input type="text" class="form-control" name="title" value=${vo.title } readonly>
</div>
.. 생략 ..
▼ 실행
'코드 정리 > Spring Framework' 카테고리의 다른 글
11. SpringMVC - 게시글 수정 및 삭제 (0) | 2023.01.30 |
---|---|
9. SpringMVC - 게시글 작성 (2) | 2023.01.29 |
8. SpringMVC - 나의 위치 정보 저장 (0) | 2023.01.29 |
7. SpringMVC - 페이징 처리 (0) | 2023.01.24 |
6. SpringMVC - Ajax 통신을 통해 아이디 중복 체크 하기 (1) | 2023.01.17 |