com.test.BoardDAO
@Test
public void getPostTest() throws ClassNotFoundException {
Class.forName(DRIVER);
String sql = "SELECT * FROM t_board"
+" WHERE bno > 0 AND bno = ?";
log.info(sql);
try(
Connection conn = DriverManager.getConnection(URL,USER,PW);
PreparedStatement pstmt = conn.prepareStatement(sql);
) {
pstmt.setInt(1, 7);
ResultSet rs = pstmt.executeQuery();
assertNotNull(rs);
rs.next();
BoardVO vo = new BoardVO();
vo.setBno(rs.getInt("bno"));
vo.setP_bno(rs.getInt("p_bno"));
vo.setTitle(rs.getString("title"));
vo.setContent(rs.getString("content"));
vo.setImgName(rs.getString("imgName"));
vo.setId(rs.getString("id"));
vo.setRegdate(rs.getDate("regdate"));
log.info("vo : " + vo);
} catch (Exception e) {
e.printStackTrace();
}
}
com.coco.dao.BoardDAO
public BoardVO getPost(int no) {
log.info("no : " + no);
String sql = "SELECT * FROM t_board"
+" WHERE bno > 0 AND bno = ?";
log.info(sql);
try(
Connection conn = ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
) {
pstmt.setInt(1, no);
ResultSet rs = pstmt.executeQuery();
rs.next();
BoardVO vo = new BoardVO();
vo.setBno(rs.getInt("bno"));
vo.setP_bno(rs.getInt("p_bno"));
vo.setTitle(rs.getString("title"));
vo.setContent(rs.getString("content"));
vo.setImgName(rs.getString("imgName"));
vo.setId(rs.getString("id"));
vo.setRegdate(rs.getDate("regdate"));
log.info("vo : " + vo);
return vo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
com.coco.service.BoardService
package com.coco.service;
import java.util.List;
import com.coco.vo.BoardVO;
public interface BoardService {
List<BoardVO> getList();
int register(BoardVO vo);
BoardVO getPost(int no);
}
package com.coco.service;
import java.util.List;
import com.coco.dao.BoardDAO;
import com.coco.vo.BoardVO;
public class BoardServiceImpl implements BoardService {
private BoardDAO dao;
public BoardServiceImpl() {
dao = new BoardDAO();
}
@Override
public List<BoardVO> getList() {
return dao.getList();
}
@Override
public int register(BoardVO vo) {
return dao.insert(vo);
}
@Override
public BoardVO getPost(int no) {
return dao.getPost(no);
}
}
com.coco.controller.BoardController
private void doHandle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BoardService boardService = new BoardServiceImpl();
String nextPage = "";
String action = request.getPathInfo();
List<BoardVO> list;
log.info("action : " + action);
if(action == null || action.equals("/list")) {
request.setAttribute("boardList", boardService.getList());
nextPage="/WEB-INF/board/list.jsp";
} else if(action.equals("/write")) {
nextPage = "/WEB-INF/board/write.jsp";
} else if (action.equals("/register")) {
String title = request.getParameter("title");
String content = request.getParameter("content");
String imgName = request.getParameter("imgName");
String id = request.getParameter("id");
BoardVO vo = new BoardVO(title, content, imgName, id);
log.info("vo : " + vo);
nextPage = boardService.register(vo) == 1
? "/board/list" : "error";
} else if(action.equals("/post")) {
int no = Integer.parseInt(request.getParameter("no"));
request.setAttribute("post", boardService.getPost(no));
nextPage="/WEB-INF/board/post.jsp";
}
RequestDispatcher dispatcher = request.getRequestDispatcher(nextPage);
dispatcher.forward(request, response);
}
post.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>[${post.bno}번 게시물] ${post.title }</title>
</head>
<body>
<h1>게시물 조회</h1>
<form action="/board/mod">
<table>
<tr>
<td width="20%" align="center"> 제목 </td>
<td> <input type='text' name='title' value='${post.title }' disabled></td>
</tr>
<tr>
<td width="20%" align="center"> 내용 </td>
<td>
<textarea rows="20" cols="60" name='content' disabled>${post.content }</textarea>
</td>
</tr>
<tr>
<td width="20%" align="center"> 작성자 </td>
<td><input type='text' name='id' value='${post.id }' disabled></td>
</tr>
<tr>
<td align="right"><input type='submit' value='수정'>
<input type='button' id='remove' value='삭제'>
<input type='button' id='list' value='목록'>
<input type='button' id='rePost' value='답글'></td>
</tr>
</table>
<input type='hidden' id='bno' name='bno' value='${bno }'>
</form>
<script>
window.onload = () => {
const remove = document.getElementById('remove')
const list = document.getElementById('list')
const rePost = document.getElementById('rePost')
console.log(remove)
console.log(list)
console.log(rePost)
list.addEventListener("click", () => {
location.replace('/board/list')
})
}
</script>
</body>
</html>