**상세보기 

=>게시판에서는 글 제목을 클릭하면 상세보기를 하도록 만듭니다.

=>상세보기를 할 때는 기본키의 값을 파라미터로 넘겨주도록 링크를 작성 

=>상세보기는 2개의 sql을 실행해야 합니다. 

=> 글번호에 해당하는 데이터의 조회수를 1증가 

=> 글번호에 해당하는 데이터를 가져와야 합니다.

<a href="링크? 이름=기본키값">제목</a>





1. list.jsp 파일에서 제목을 출력하는 부분을 링크를 추가 

<a href="detail?bno=${vo.bno}">${vo.title}</a>



2. 글번호 에 해당하는 데이터를 가져와야합니다. 

<!-- 상세보기를 할때 글 번호에 해당하는 데이터의 조회수를 1증가시켜주는 sql -->

<update id="updatecnt" parameterType="java.lang.Integer">


update springboard

set readcnt = readcnt +1

where bno=#{bno}


</update>

3.상세보기를 할 때 글번호에 해당하는 데이터를 가져오는 sql 


<select id="detail" parameterType="java.lang.Integer" resultType="Board">

select * 

from springboard

where bno = #{bno}

</select>



3. BoardDao 클래스에 글번호에 해당하는 데이터의 조회수를 1증가시켜 주는 

메소드와 글번호에 해당하는 데이터를 가져오는 메소드를 생성 


public void updatecnt(int bno){

sqlSession.update("board.updatecnt",bno);

}

//글번호에 해당하는 데이터를 가져오는 메소드 

public Board detail(int bno) {

return sqlSession.selectOne("board.detail",bno);

}


4. BoardService 인터페이스에 상세보기를 처리할 메소드를 선언 

public Board detail(HttpServletRequest request) ;



5.BoardServiceImpl 클래스에 상세보기를 처리할 메소드를 구현 


@Override

public Board detail(HttpServletRequest request) {

//파라미터 읽기 

//Integer.parsint를 호출했을 때 예외가 생기는 경우 

//=> null이 리턴되서 정수로 변환을 못하는 경우 

//=> 숫자 뒤에 공백이 포함되 정수로 변환을 못하는 경우 

//NumberFormatException이 발생하면 Integer.parseint에 대입한 데이터를 먼저 출력해서 확인 

String bno  = request.getParameter("bno");

//조회수 1증가시키는 메소드 호출 

boardDao.updatecnt(Integer.parseInt(bno));

//데이터 가져오는 메소드를 호출해서 리턴 

return boardDao.detail(Integer.parseInt(bno));

}


5.BoardController에 상세보기 요청을 처리하는 메소드를 구현 

@Reqeustparam(value = "board/detail", method=Reqeustparam.GET)

public String detail(HttpServletRequest request ,Model model){

Board board = boardService.detail(request);

model.addAttribute("board", board);

return "board/detail";


}



7.views.board 디렉토리에 detail.jsp 파일을 만들고 출력 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>상세보기</title>

</head>

<body>

${vo}

<%@ include file="../include/header.jsp" %>


<section class="content">

<div class="box">

<div class="box-header">

<h3 class="box-title">상세보기</h3>

</div>

<div class="box-body">

<div class="form-group">

<label>제목</label>

<input type="text" name="title"

class="form-control" value="${vo.title}"

readonly="readonly" />

</div>

<div class="form-group">

<label>내용</label>

<textarea name="content" rows="5"

readonly="readonly" class="form-control">${vo.content}</textarea>

</div>

<div class="form-group">

<label>작성자</label>

<input type="text" 

class="form-control" value="${vo.nickname}"

readonly="readonly" />

</div>

</div>

<div class="box-footer">

<button class="btn btn-success" id="mainbtn">메인</button>

<c:if test = "${user.email == vo.email}">

<button class="btn btn-warning" id="updatebtn">수정</button>

<button class="btn btn-danger" id="deletebtn">삭제</button>

</c:if>

<button class="btn btn-primary" id="listbtn">목록</button>

</div>

</div>

</section>

<%@ include file="../include/footer.jsp" %>

<script>

//메인 버튼을 눌렀을 때 처리

document.getElementById("mainbtn").addEventListener("click", function(){

location.href="../";

});

//목록 버튼을 눌렀을 때 처리

document.getElementById("listbtn").addEventListener("click", function(){

location.href="list";

});

<c:if test = "${user.email == vo.email}">

//삭제 버튼을 눌렀을 때 처리

document.getElementById("deletebtn").addEventListener("click", function(){

location.href="delete?bno=" + ${vo.bno};

});

//수정 버튼을 눌렀을 때 처리

document.getElementById("updatebtn").addEventListener("click", function(){

location.href="update?bno=" + ${vo.bno};

});

</c:if>

</script>

</body>

</html>


+ Recent posts