728x90
스프링 게시판 이전 글 , 다음 글 구현하기
0. 이슈 발생
게시판에서 이전 글과 다음 글을 보여주는 기능 구현해보기
728x90
1. JSP (document_detail.jsp)
<!---------------------- 이전/다음글 ----------------------------------->
<c:forEach var="listLevel" items="${nextList}">
<table id="nextBoard">
<tr>
<td>이전</td>
<td>
<a href="/community/notice_detail?IN_SEQ=${listLevel.PREV_NUM}">${listLevel.PREV_TITLE}</a>
<c:if test="${empty listLevel.PREV_TITLE}">
<a href="/community/notice">이전글이 없습니다.</a>
</c:if>
</td>
</tr>
<tr style="border-top: 1px solid #dbdbdb;">
<td>다음</td>
<td>
<a href="/community/notice_detail?IN_SEQ=${listLevel.NEXT_NUM}">${listLevel.NEXT_TITLE}</a>
<c:if test="${empty listLevel.NEXT_TITLE}">
<a href="/community/notice">다음글이 없습니다.</a>
</c:if>
</td>
</tr>
</table>
</c:forEach>
- 데이터가 없을 경우(empty) 없습니다 표시해주기
#nextBoard{
border-top: 1px solid #dbdbdb;
border-bottom: 1px solid #dbdbdb;
width: 1180px;
margin-left: 20%;
}
#nextBoard tr{
height: 60px;
}
#nextBoard td:nth-child(1){
width: 120px;
font-size: 14px;
line-height: 1.07;
text-align: center;
color: #767676;
}
#nextBoard td:nth-child(2){
width: 880px;
font-size: 16px;
padding: 17px 98px 17px 32px;
}
2. Mapper
<select id="selectBoardNextList" parameterType="hashmap" resultType="hashmap">
<![CDATA[
SELECT *
FROM (
SELECT IN_SEQ,
LEAD(IN_SEQ) OVER (ORDER BY IN_SEQ) AS NEXT_NUM,
LAG(IN_SEQ) OVER (ORDER BY IN_SEQ) AS PREV_NUM,
VC_TITLE,
LEAD(VC_TITLE) OVER (ORDER BY IN_SEQ) AS NEXT_TITLE,
LAG(VC_TITLE) OVER (ORDER BY IN_SEQ) AS PREV_TITLE
FROM TBL_BOARD_INFO
WHERE IN_TYPE_CODE = #{IN_TYPE_CODE} AND CH_DEL_YN = 'N'
) AS TEMP
WHERE IN_SEQ = #{IN_SEQ}
]]>
</select>
- LEAD : 이전 행 값 리턴 함수 / LAG : 다음 행 값 리턴 함수
3. DAO
@SuppressWarnings("unchecked")
public List<Map<String, Object>> selectBoardNextList(Map<String, Object> map) throws Exception{
return (List<Map<String, Object>>)selectList("board.selectBoardNextList", map);
}
4. Service
/**
* <pre>
* 게시판 이전글-다음글
* </pre>
* @param Map<String, Object> map
* @return List
* @throws Exception
*/
List<Map<String, Object>> selectBoardNextList(Map<String, Object> map) throws Exception;
5. ServiceImpl
@Override
public List<Map<String, Object>> selectBoardNextList(Map<String, Object> map) throws Exception {
return boardDAO.selectBoardNextList(map);
}
6. Controller
/**
* [공지사항] 상세페이지
*
*/
@RequestMapping(value="/community/notice_detail")
public ModelAndView notice_detail(CommandMap commandMap, HttpServletRequest request) throws Exception{
ModelAndView mv = new ModelAndView("/pc/community/notice_detail");
Map<String, Object> map = new HashMap<String, Object>();
map.put("IN_SEQ", commandMap.get("IN_SEQ"));
// 게시판 조회수 업데이트
boardService.updateBoardHit(map);
if (commandMap.containsKey("IN_SEQ")) {
Map<String,Object> boardDetail = boardService.selectBoardDetail(map);
mv.addObject("boardDetail", boardDetail);
// 파일 목록
commandMap.put("IN_BOARD_SEQ", commandMap.get("IN_SEQ"));
List<Map<String,Object>> boardFiles = boardService.selectBoardFileList(commandMap.getMap());
mv.addObject("boardFiles", boardFiles);
// 이전글+다음글
commandMap.put("IN_TYPE_CODE", "4001");
List<Map<String,Object>> nextList = boardService.selectBoardNextList(commandMap.getMap());
mv.addObject("nextList", nextList);
}
return mv;
}
7. 완성!
300x250
'CODING > 🍃Spring & Spring Boot' 카테고리의 다른 글
[Spring] 검색 기능 구현하기 (0) | 2023.12.07 |
---|---|
[Spring] 스프링 주요 어노테이션 정리 (0) | 2023.11.06 |
[Spring] 파라미터 존재 여부 확인하기 (0) | 2023.09.13 |
[Spring] Lombok.jar 다운 및 설치하기 (0) | 2023.08.23 |
[Spring] Spring 다운 및 환경 설정 (0) | 2023.08.22 |