위로
아래
글 내용 불러오기 매커니즘
글 내용을 불러오기 위해서는, 데이터베이스에 접속해 subject, writer, content, passwd를 불러와야 한다.
매커니즘은 다음과 같다.
- 데이터 전송을 위한 DTO 객체 생성
- 데이터베이스에 접속하기 위한 DAO 객체 생성
- 불러올 게시글의 bno 파라미터를 저장
- DAO의 getArticle() 메소드를 사용해 DTO 객체에 값 저장하기
- DTO 속성 설정 후, RequestDispatch를 통해 글 내용 보기 페이지로 DTO를 지닌 채 이동
DTO와 DAO 객체 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "bean.BoardDTO" %>
<%@ page import = "bean.BoardDAO" %>
<%
BoardDTO dto = new BoardDTO();
BoardDAO dao = BoardDAO.getInstance();
%>
DTO는 생성자에 바로 접근할 수 있는 반면, DAO는 불가능하다. 따라서 DAO는 getInstance() 메소드를 통해 객체를 생성한다.
DAO를 통해 DTO 객체의 값 받아오기
<%
int bno = Integer.parseInt(request.getParameter("bno"));
dto = dao.getArticle(bno);
%>
파라미터는 String 형태로 들어오기 때문에, parseInt로 int형으로 만들어주어야 한다.
parseInt를 쓰기 위해서 포장 클래스인 Integer를 사용한다.
DTO 객체를 지닌 채 페이지 이동
<%
request.setAttribute("dto", dto);
RequestDispatcher dp = request.getRequestDispatcher("../view/BoardContent.jsp");
dp.forward(request, response);
%>
RequestDispatcher를 이용해 DTO에 담긴 데이터를 가지고 페이지 이동
전체 코드
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "bean.BoardDTO" %>
<%@ page import = "bean.BoardDAO" %>
<%
BoardDTO dto = new BoardDTO();
BoardDAO dao = BoardDAO.getInstance();
int bno = Integer.parseInt(request.getParameter("bno"));
dto = dao.getArticle(bno);
request.setAttribute("dto", dto);
RequestDispatcher dp = request.getRequestDispatcher("../view/BoardContent.jsp");
dp.forward(request, response);
%>