Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
관리 메뉴

개(발)린이

Spring 게시판 3 본문

Spring

Spring 게시판 3

불도정 2023. 5. 2. 20:00

우선 currentPage가 잘 작동하게끔 코드를 수정한다.

 

boardList.jsp이다

 <!-- 페이지네이션 a태그에 사용될 공통 주소를 저장한 변수 선언 -->
                <c:set var="url" value="${boardCode}?cp="/>

이로서 페이지네이션은 작동한다.

 

 

 

 

잘 작동되는 모습을 볼 수 있다.

 

게시글 상세조회를 해보자

우선 boardList.jsp에 링크를 고쳐보자

  <c:otherwise>
                                <!-- 게시글 목록 조회 결과가 비어있지 않다면 -->

                                <!-- 향상된 for문처럼 사용 -->
                                <c:forEach var="board" items="${boardList}">
                                    <tr>
                                        <td>${board.boardNo}</td>
                                        <td> 
                                            <c:if test="${!empty board.thumbnail}">
                                                <img class="list-thumbnail" src="${contextPath}${board.thumbnail}">
                                            </c:if>  

                                            <a href="../detail/${boardCode}/${board.boardNo}?cp=${pagination.currentPage}${sURL}">${board.boardTitle}</a>   
                                            <%--detail?no=${board.boardNo}&cp=${pagination.currentPage}&type=${param.type}${sURL} --%>                        
                                        
                                        	<!-- 현재 페이지 주소 : /board/list/1?cp=1
                                        		 상세 조회 주소 : /board/detail/1/300?cp=1
                                        	 -->
                                        </td>
                                        <td>${board.memberNickname}</td>
                                        <td>${board.createDate}</td>
                                        <td>${board.readCount}</td>
                                    </tr>
                                </c:forEach>

                            </c:otherwise>

게시판을 들어가면 board/list/{boardCode}를 보고 있다.

게시글을 클릭하면 한단계 위로 올라가서 

board/detail/1/300?cp=1으로 이동해야하므로 링크 url을 바꾼다.

 

다음엔 BoardController 클래스로 가서 메소드를 만들어 준다.

@GetMapping("/detail/{boardCode}/{boardNo}")
	public String boardDetail(@PathVariable("boardCode") int boardCode,
							  @PathVariable("boardNo") int boardNo,
							  @RequestParam(value = "cp", required = false, defaultValue = "1") int cp,
							  Model model ) {
		
		// 게시글 상세 조회 서비스 호출
		BoardDetail detail = service.selectBoardDetail(boardNo);
		
        model.addAttribute("detail", detail);
        
        return "board/boardDetail";
	}

 

Service 인터페이스로 가서 메소드를 만든다.

/** 게시글 상세조회 서비스
	 * @param boardNo
	 * @return detail
	 */
	BoardDetail selectBoardDetail(int boardNo);

 

 

다음으론 ServiceImpl 클래스로 가서 오버라이딩 해준다.

@Service
public class BoardServiceImpl implements BoardService {

	@Autowired
	private BoardDAO dao;
	
	// 게시판 코드, 이름 조회
	@Override
	public List<BoardType> selectBoardType() {
		
		return dao.selectBoardType();
	}
	
	// 게시글 목록조회 서비스 구현
	@Override
	public Map<String, Object> selectBoardList(int cp, int boardCode) {
		// 1) 게시판 이름 조회 -> 인터셉터 application에 올려둔 boardTypeList 쓸 수 있음
		
		// 2) 페이지네이션 객체 생성(listCount)
		int listCount = dao.getListCount(boardCode);
		Pagination pagination = new Pagination(cp, listCount);
		
		// 3) 게시글 목록 조회
		List<Board> boardList = dao.selectBoardList(pagination, boardCode);
		
		// map 만들어 담기
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pagination", pagination);
		map.put("boardList", boardList);
		map.put("boardCode", boardCode);
		
		return map;
	}
	
	// 게시글 상세 조회 서비스 구현
	
	@Override
	public BoardDetail selectBoardDetail(int boardNo) {
		
		return dao.selectBoardDetail(boardNo);
	}
}

 

다음음으로 DAO

	public BoardDetail selectBoardDetail(int boardNo) {
		
		return sqlSession.selectOne("boardMapper.selectBoardDetail", boardNo);
	}

 

마지막으로 board-mapper.xml이다

 

 

실행해보면

게시글이 잘 출력되는것을 볼 수 있다.

 

 

'Spring' 카테고리의 다른 글

Spring(scheduling)  (0) 2023.05.11
Spring (게시글 조회수)  (0) 2023.05.07
Spring 게시판 테이블  (0) 2023.05.01
Spring 게시판 2  (0) 2023.05.01
Spring 게시판  (0) 2023.05.01