공부한것들을 정리하는 블로그 입니다.
7-8. 글 등록, 수정, 삭제 구현 : insertBoard.jsp, insertBoard_proc.jsp, updateBoard_proc.jsp, deleteBoard_proc.jsp 본문
7-8. 글 등록, 수정, 삭제 구현 : insertBoard.jsp, insertBoard_proc.jsp, updateBoard_proc.jsp, deleteBoard_proc.jsp
호 두 2019. 7. 12. 10:23프로젝트 별첨 https://drsggg.tistory.com/233
1. index.jsp 및 getBoardList.jsp
2. BoardDAO.java 수정
3. 글 등록 화면 및 기능 추가 : insertBoard.jsp, insertBoard_proc.jsp
4. 글 수정 기능 추가 : updateBoard_proc.jsp
5. 글 삭제 기능 추가 : deleteBoard_proc.jsp
1. index.jsp 및 getBoardList.jsp
- 기존에 페이징처리가 되지 않은 getBoardList.jsp 를 제거
- 페이징 처리가 완료 된 getBoardList2.jsp 를 getBoardList.jsp 로 대체
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Main Page</title>
</head>
<body>
<center>
<h1>게시판 프로그램</h1>
<hr>
<a href="login.jsp">로그인</a><br><br><br>
<!-- <a href="getBoardList2.jsp">글 목록 바로가기</a><br><br><br> -->
<a href="getBoardList.jsp">(페이징)글 목록 바로가기</a><br><br><br>
<hr>
</center>
</body>
</html>
getBoardList.jsp
- 하단의 새글등록 버튼 클릭 시 insert.jsp 로 이동
<%@ page import="java.io.Console"%>
<%@ page import="java.util.List"%>
<%@ page import="com.springbook.biz.board.impl.BoardDAO"%>
<%@ page import="com.springbook.biz.board.BoardVO"%>
<%@ page import="com.springbook.biz.user.UserVO"%>
<%@ page import="com.springbook.biz.paging.PagingVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
// 한글깨짐방지
request.setCharacterEncoding("UTF-8");
// 사용자 이름 설정
String id = (String)session.getAttribute("id");
// 임의의 상수 설정
String action = "/biz/getBoardList.jsp";
String searchCondition = request.getParameter("searchCondition");
String searchKeyword = request.getParameter("searchKeyword");
String search = "&searchCondition="+searchCondition+"&searchKeyword="+searchKeyword;
// 쿠키
//session.setAttribute("cookie", request.getCookies());
//========= get board list with paging ===========
BoardVO vo = new BoardVO();
BoardDAO boardDAO = new BoardDAO();
// 게시글 검색 설정
vo.setSearchCondition(searchCondition);
vo.setSearchKeyword(searchKeyword);
// 페이징 처리 된 데이터 조회
int boardListcnt = boardDAO.getBoardCnt(vo);
//========= paging ===========
PagingVO pagingVO = new PagingVO();
// 현재 페이지 체크 및 설정
// 첫 페이지면 1이 설정, 아니라면 해당 페이지 넘버가 설정됨
int currentPage = 1;
//System.out.println("request.param page = " + request.getParameter("page") );
if (request.getParameter("page") != null) {
currentPage = Integer.parseInt(request.getParameter("page"));
}
pagingVO.setCurrentPage(currentPage);
// 전체 페이지 설정
// sql rownum 설정하기 이전의 count 필요
pagingVO.setTotalCount( boardListcnt );
//========= pagination ===========
pagingVO.setEndPage( ((int) Math.ceil(pagingVO.getCurrentPage() / (double) pagingVO.getDisplayPage())) * pagingVO.getDisplayPage() ); //Math.ceil : 소수점 이하를 올림한다
pagingVO.setBeginPage( pagingVO.getEndPage() - (pagingVO.getDisplayPage() - 1) );
pagingVO.setTotalPage( (int) Math.ceil(pagingVO.getTotalCount() / (double) pagingVO.getDisplayRow()) );
if (pagingVO.getEndPage() > pagingVO.getTotalPage()) {
pagingVO.setEndPage(pagingVO.getTotalPage());
}
//========= get board list with paging ===========
// 페이징 처리 된 데이터 조회
List<BoardVO> boardList;
boardList = boardDAO.getBoardListWithPaging(currentPage, vo);
System.out.println("currentPage = " + currentPage );
System.out.println("999 pagingVO.toString() = " + pagingVO.toString() );
System.out.println("000 boardList.toString() = " + boardList.toString() );
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글 목록</title>
<style type="text/css">
body {
text-align: center;
}
#paging {
font-size: 22pt;
}
</style>
</head>
<body>
<center>
<h1>글 목록</h1>
<h3><%=id%>님 환영합니다....<a href="logout_proc.jsp">log out</a>
</h3>
<!-- 검색 시작 -->
<!-- <form action="getBoardList.do" method="get"> -->
<form action="getBoardList.jsp" method="get">
<table border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
<td align="right"><select id="searchCondition"
name="searchCondition">
<option value="TITLE">제목</option>
<option value="CONTENT">내용</option>
</select> <input id="searchKeyword" name="searchKeyword" type="text">
<input type="submit" value="검색 "></td>
</tr>
</table>
</form>
<!-- 검색 종료 -->
<!-- 전체 게시글 수 -->
<div>
전체 게시글 수 : <%=pagingVO.getTotalCount() %>
</div>
<table border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
<th bgcolor="orange" width="100">번호</th>
<th bgcolor="orange" width="200">제목</th>
<th bgcolor="orange" width="150">작성자</th>
<th bgcolor="orange" width="150">등록일</th>
<th bgcolor="orange" width="100">조회수</th>
</tr>
<%
for (BoardVO board : boardList) {
%>
<tr>
<td><%=board.getSeq()%></td>
<td align="left"><a href="getBoard.jsp?seq=<%=board.getSeq()%>">
<%=board.getTitle()%>
</a></td>
<td><%=board.getWriter()%></td>
<td><%=board.getRegDate()%></td>
<td><%=board.getCnt()%></td>
</tr>
<%
}
%>
</table>
<br>
<!-- 여기부터 페이징처리 -->
<div id="pagination">
<!-- 변수 매핑 -->
<c:set var="page" value="<%=pagingVO.getCurrentPage() %>" />
<c:set var="beginPage" value="<%=pagingVO.getBeginPage() %>" />
<c:set var="endPage" value="<%=pagingVO.getEndPage() %>" />
<c:set var="totalPage" value="<%=pagingVO.getTotalPage() %>" />
<c:set var="action" value="<%=action %>" />
<c:set var="displayPage" value="<%=pagingVO.getDisplayPage() %>" />
<c:set var="searchKeyword" value="<%=searchKeyword %>" />
<c:set var="search" value="<%=search %>" />
<!-- 처음으로 -->
<c:if test="${ searchKeyword == null }">
<a href="${ action }?page=1">
<span>«</span>
</a>
</c:if>
<c:if test="${ searchKeyword != null }">
<a href="${ action }?page=1${ search }">
<span>«</span>
</a>
</c:if>
<!-- 이전버튼 -->
<c:if test="${ page <= 1 }">
<span>이전</span>
</c:if>
<c:if test="${ page > 1 }">
<c:if test="${ searchKeyword == null }">
<a href="${ action }?page=${ page - 1 }">이전</a>
</c:if>
<c:if test="${ searchKeyword != null }">
<a href="${ action }?page=${ page - 1 }${ search }">이전</a>
</c:if>
</c:if>
<!-- 넘버링버튼 for문 -->
<c:forEach var="item" varStatus="status" begin="${ beginPage }" end="${ endPage }" step="1">
<c:if test="${ page == item }">
${ item }
</c:if>
<c:if test="${ page != item }">
<c:if test="${ searchKeyword == null }">
<a href="${ action }?page=${ item }">${ item }</a>
</c:if>
<c:if test="${ searchKeyword != null }">
<a href="${ action }?page=${ item }${ search }">${ item }</a>
</c:if>
</c:if>
</c:forEach>
<!-- 다음버튼 -->
<c:if test="${ page >= totalPage }">
<span>다음</span>
</c:if>
<c:if test="${ page < totalPage }">
<c:if test="${ searchKeyword == null }">
<a href="${ action }?page=${ page + 1 }">다음</a>
</c:if>
<c:if test="${ searchKeyword != null }">
<a href="${ action }?page=${ page + 1 }${ search }">다음</a>
</c:if>
</c:if>
<!-- 끝으로 -->
<c:if test="${ searchKeyword == null }">
<a href="${ action }?page=${ totalPage }">
<span>»</span>
</a>
</c:if>
<c:if test="${ searchKeyword != null }">
<a href="${ action }?page=${ totalPage }${ search }">
<span>»</span>
</a>
</c:if>
</div>
<br> <a href="insertBoard.jsp">새 글 등록</a>
</center>
</body>
</html>
2. BoardDAO.java 수정
- 글 등록 기능 관련하여 BOARD_INSERT 추가
- 글 수정 기능 관련하여 BOARD_UPDATE 추가
- 글 삭제 기능 관련하여 BOARD_DELETE 추가
package com.springbook.biz.board.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.JDBCUtil;
import com.springbook.biz.paging.PagingVO;
import hello.printSimpleName;
@Repository("boardDAO")
public class BoardDAO {
private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet rs = null;
private final String BOARD_INSERT = "insert into board(seq, title, writer, content, regdate) values((select nvl(max(seq),0)+1 from board),?,?,?,sysdate)";
private final String BOARD_UPDATE = "update board set title=?, content=? where seq=?";
private final String BOARD_DELETE = "delete board where seq=?";
private final String BOARD_GET = "select * from board where seq=?";
// private final String BOARD_LIST = "select * from board order by seq desc";
private final String BOARD_LIST = "select * from board order by seq desc";
private final String BOARD_LIST_T = "select * from board where title like '%'||?||'%' order by seq desc";
private final String BOARD_LIST_C = "select * from board where content like '%'||?||'%' order by seq desc";
private final String BOARD_UPDAT_CNT = "update board set cnt=cnt+1 where seq=?";
private final String BOARD_LIST2_WITH_PAGING = "SELECT * from ( SELECT ROWNUM AS row_num, board.* FROM ( SELECT * FROM board ORDER BY seq DESC ) board ) WHERE row_num >= ? AND row_num <= ?";
private final String BOARD_LIST_T2_WITH_PAGING = "SELECT * from ( SELECT ROWNUM AS row_num, board.* FROM ( SELECT * FROM board WHERE TITLE LIKE '%'||?||'%' ORDER BY seq DESC ) board ) WHERE row_num >= ? AND row_num <= ?";
private final String BOARD_LIST_C2_WITH_PAGING = "SELECT * from ( SELECT ROWNUM AS row_num, board.* FROM ( SELECT * FROM board WHERE CONTENT LIKE '%'||?||'%' ORDER BY seq DESC ) board ) WHERE row_num >= ? AND row_num <= ?";
private final String BOARD_LIST2_CNT = "SELECT COUNT(*) FROM ( SELECT * FROM board ORDER BY seq DESC ) board";
private final String BOARD_LIST_T2_CNT = "SELECT COUNT(*) from ( SELECT ROWNUM AS row_num, board.* FROM ( SELECT * FROM board ORDER BY seq DESC ) board ) WHERE TITLE LIKE '%'||?||'%'";
private final String BOARD_LIST_C2_CNT = "SELECT COUNT(*) from ( SELECT ROWNUM AS row_num, board.* FROM ( SELECT * FROM board ORDER BY seq DESC ) board ) WHERE CONTENT LIKE '%'||?||'%'";
printSimpleName p = new printSimpleName();
public void insertBoard(BoardVO vo) {
System.out.println("===> JDBC로 insertBoard() 기능 처리");
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_INSERT);
stmt.setString(1, vo.getTitle());
stmt.setString(2, vo.getWriter());
stmt.setString(3, vo.getContent());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
}
public void updateBoard(BoardVO vo) {
System.out.println("===> JDBC로 updateBoard() 기능 처리");
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_UPDATE);
stmt.setString(1, vo.getTitle());
stmt.setString(2, vo.getContent());
stmt.setInt(3, vo.getSeq());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
}
public void deleteBoard(BoardVO vo) {
System.out.println("===> JDBC로 deleteBoard() 기능 처리");
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_DELETE);
stmt.setInt(1, vo.getSeq());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
}
public BoardVO getBoard(BoardVO vo) {
System.out.println("===> JDBC로 getBoard() 기능 처리");
BoardVO board = null;
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_GET);
stmt.setInt(1, vo.getSeq());
rs = stmt.executeQuery();
if(rs.next()) {
board = new BoardVO();
board.setSeq(rs.getInt("SEQ"));
board.setTitle(rs.getString("TITLE"));
board.setWriter(rs.getString("WRITER"));
board.setContent(rs.getString("CONTENT"));
board.setRegDate(rs.getDate("REGDATE"));
board.setCnt(rs.getInt("CNT"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
return board;
}
public List<BoardVO> getBoardList(BoardVO vo) {
System.out.println("===> JDBC로 getBoardList() 기능 처리");
List<BoardVO> boardList = new ArrayList<BoardVO>();
String flagBoardListFromSearch = "N";
try {
conn = JDBCUtil.getConnection();
if (vo.getSearchCondition() != null && vo.getSearchKeyword() != null) {
flagBoardListFromSearch = "Y";
}
if("Y".equals(flagBoardListFromSearch) && vo.getSearchCondition().equals("TITLE")) {
stmt = conn.prepareStatement(BOARD_LIST_T);
stmt.setString(1, vo.getSearchKeyword());
} else if("Y".equals(flagBoardListFromSearch) && vo.getSearchCondition().equals("CONTENT") ) {
stmt = conn.prepareStatement(BOARD_LIST_C);
stmt.setString(1, vo.getSearchKeyword());
} else {
stmt = conn.prepareStatement(BOARD_LIST);
}
rs = stmt.executeQuery();
while(rs.next()) {
BoardVO board = new BoardVO();
board.setSeq(rs.getInt("SEQ"));
board.setTitle(rs.getString("TITLE"));
board.setWriter(rs.getString("WRITER"));
board.setContent(rs.getString("CONTENT"));
board.setRegDate(rs.getDate("REGDATE"));
board.setCnt(rs.getInt("CNT"));
boardList.add(board);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
return boardList;
}
public void updateBoardCnt(BoardVO vo) {
System.out.println("===> JDBC로 updateBoardCnt() 기능 처리");
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_UPDAT_CNT);
stmt.setInt(1, vo.getSeq());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
}
public List<BoardVO> getBoardListWithPaging(int currentPage, BoardVO vo){
System.out.println("===> JDBC로 getBoardListWithPaging() 기능 처리");
PagingVO paging = new PagingVO();
List<BoardVO> boardList = new ArrayList<BoardVO>();
String flagBoardListFromSearch = "N";
int startNum = (currentPage - 1) * paging.getDisplayRow() + 1;
int endNum = currentPage * paging.getDisplayRow();
//System.out.println("===> startNum = " + startNum );
//System.out.println("===> endNum = " + endNum );
try{
conn = JDBCUtil.getConnection();
if (vo.getSearchCondition() != null && vo.getSearchKeyword() != null) {
flagBoardListFromSearch = "Y";
}
//페이징
if("Y".equals(flagBoardListFromSearch) && vo.getSearchCondition().equals("TITLE")) {
stmt = conn.prepareStatement(BOARD_LIST_T2_WITH_PAGING);
stmt.setString(1, vo.getSearchKeyword());
stmt.setInt(2, startNum);
stmt.setInt(3, endNum);
} else if("Y".equals(flagBoardListFromSearch) && vo.getSearchCondition().equals("CONTENT") ) {
stmt = conn.prepareStatement(BOARD_LIST_C2_WITH_PAGING);
stmt.setString(1, vo.getSearchKeyword());
stmt.setInt(2, startNum);
stmt.setInt(3, endNum);
} else {
stmt = conn.prepareStatement(BOARD_LIST2_WITH_PAGING);
stmt.setInt(1, startNum);
stmt.setInt(2, endNum);
}
rs = stmt.executeQuery();
while(rs.next()){
BoardVO board = new BoardVO();
board.setSeq(rs.getInt("SEQ"));
board.setTitle(rs.getString("TITLE"));
board.setWriter(rs.getString("WRITER"));
board.setContent(rs.getString("CONTENT"));
board.setRegDate(rs.getDate("REGDATE"));
board.setCnt(rs.getInt("CNT"));
boardList.add(board);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.close(stmt, conn);
}
return boardList;
}
// 게시글 수 조회
public int getBoardCnt(BoardVO vo) {
System.out.println("===> JDBC로 getBoardCnt() 기능 처리");
String flagBoardListFromSearch = "N";
int boardListcnt = 0;
try{
conn = JDBCUtil.getConnection();
if (vo.getSearchCondition() != null && vo.getSearchKeyword() != null) {
flagBoardListFromSearch = "Y";
}
//페이징
if("Y".equals(flagBoardListFromSearch) && vo.getSearchCondition().equals("TITLE")) {
stmt = conn.prepareStatement(BOARD_LIST_T2_CNT);
stmt.setString(1, vo.getSearchKeyword());
} else if("Y".equals(flagBoardListFromSearch) && vo.getSearchCondition().equals("CONTENT") ) {
stmt = conn.prepareStatement(BOARD_LIST_C2_CNT);
stmt.setString(1, vo.getSearchKeyword());
} else {
stmt = conn.prepareStatement(BOARD_LIST2_CNT);
}
rs = stmt.executeQuery();
if(rs.next()){
boardListcnt = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
return boardListcnt;
}
}
3. 글 등록 화면 및 기능 추가 : insertBoard.jsp, insertBoard_proc.jsp
insertBoard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>새 글 등록</title>
</head>
<body>
<center>
<h1>글 등록</h1>
<a href="logout_proc.jsp">log out</a>
<hr>
<form action="insertBoard_proc.jsp" method="post">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange" width="70">제목</td>
<td align="left">
<input name="title" type="text">
</td>
</tr>
<tr>
<td bgcolor="orange">작성자</td>
<td align="left">
<input name="writer" size="10" />
</td>
</tr>
<tr>
<td bgcolor="orange">내용</td>
<td align="left">
<textarea name="content" cols="40" rows="10"></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="새 글 등록" />
</td>
</tr>
</table>
</form>
<hr>
<a href="getBoardList.jsp">글 목록 가기</a>
</center>
</body>
</html>
insertBoard_proc.jsp
- 글 등록 후에는 getBoardList.jsp 로 이동합니다(redirect)
<%@ page import="com.springbook.biz.board.impl.BoardDAO" %>
<%@ page import="com.springbook.biz.board.BoardVO" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String content = request.getParameter("content");
BoardVO vo = new BoardVO();
vo.setTitle(title);
vo.setWriter(writer);
vo.setContent(content);
BoardDAO boardDAO = new BoardDAO();
boardDAO.insertBoard(vo);
response.sendRedirect("getBoardList.jsp");
%>
getBoardList.jsp
- 글 등록 후에는 getBoardList.jsp 로 이동합니다(redirect)
4. 글 수정 기능 추가 : updateBoard_proc.jsp
getBoard.jsp
- 글 목록(getBoardList.jsp) 페이지에서 글 상세(getBoard.jsp) 페이지로 이동
<%@ page import="com.springbook.biz.board.impl.BoardDAO" %>
<%@ page import="com.springbook.biz.board.BoardVO" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 저장된 쿠키 불러오기
Cookie[] cookieFromRequest = request.getCookies();
String cookieValue = null;
for(int i = 0 ; i<cookieFromRequest.length; i++) {
// 요청정보로부터 쿠키를 가져온다.
cookieValue = cookieFromRequest[0].getValue(); // 테스트라서 추가 데이터나 보안사항은 고려하지 않으므로 1번째 쿠키만 가져옴
}
// 글 목록 -> 글 상세 : 글번호(seq)
String seq = request.getParameter("seq");
// 쿠키 세션 입력
if (session.getAttribute(seq+":cookie") == null) {
session.setAttribute(seq+":cookie", seq + ":" + cookieValue);
} else {
session.setAttribute(seq+":cookie ex", session.getAttribute(seq+":cookie"));
if (!session.getAttribute(seq+":cookie").equals(seq + ":" + cookieValue)) {
session.setAttribute(seq+":cookie", seq + ":" + cookieValue);
}
}
BoardVO vo = new BoardVO();
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
// 글 상세 조회
BoardVO board = boardDAO.getBoard(vo);
// 조회수 카운트
if (!session.getAttribute(seq+":cookie").equals(session.getAttribute(seq+":cookie ex"))) {
boardDAO.updateBoardCnt(vo);
// 가시적으로 조회수 1 추가해줌
board.setCnt(board.getCnt() + 1);
}
//System.out.println("중복방지 111 = " + session.getAttribute(seq+":cookie") );
//System.out.println("중복방지 222 = " + session.getAttribute(seq+":cookie ex") );
//System.out.println("중복방지 333 = " + session.toString() );
//for(int i = 0; i < session.getValueNames().length; i++){
// System.out.println("중복방지 444 = " + session.getValueNames()[i].toString() );
//}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글 상세</title>
</head>
<body>
<center>
<h1>글 상세</h1>
<a href="logout_proc.jsp">log out</a>
<hr>
<form action="updateBoard_proc.jsp" method="post">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange" width="70">제목</td>
<td align="left">
<input name="title" type="text" value="<%= board.getTitle() %>">
</td>
</tr>
<tr>
<td bgcolor="orange">작성자</td>
<td align="left"><%= board.getWriter() %></td>
</tr>
<tr>
<td bgcolor="orange">내용</td>
<td align="left">
<textarea name="content" cols="40" rows="10"><%= board.getContent() %></textarea>
</td>
</tr>
<tr>
<td bgcolor="orange">등록일</td>
<td align="left"><%= board.getRegDate() %></td>
</tr>
<tr>
<td bgcolor="orange">조회수</td>
<td align="left"><%= board.getCnt() %></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="글 수정" />
</td>
</tr>
</table>
<input name="seq" type="hidden" value="<%= board.getSeq() %>" />
</form>
<hr>
<a href="insertBoard.jsp">글 등록</a>
<a href="deleteBoard_proc.jsp?seq=<%= board.getSeq() %>">글 삭제</a>
<a href="getBoardList.jsp">글 목록</a>
</center>
</body>
</html>
글 수정 후 글수정 버튼 클릭시 getBoardList.jsp로 이동
<%@ page import="com.springbook.biz.board.impl.BoardDAO" %>
<%@ page import="com.springbook.biz.board.BoardVO" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 저장된 쿠키 불러오기
Cookie[] cookieFromRequest = request.getCookies();
String cookieValue = null;
for(int i = 0 ; i<cookieFromRequest.length; i++) {
// 요청정보로부터 쿠키를 가져온다.
cookieValue = cookieFromRequest[0].getValue(); // 테스트라서 추가 데이터나 보안사항은 고려하지 않으므로 1번째 쿠키만 가져옴
}
// 글 목록 -> 글 상세 : 글번호(seq)
String seq = request.getParameter("seq");
// 쿠키 세션 입력
if (session.getAttribute("seq:cookie") == null) {
session.setAttribute("seq:cookie", seq + ":" + cookieValue);
} else {
session.setAttribute("seq:cookie ex", session.getAttribute("seq:cookie"));
if (!session.getAttribute("seq:cookie").equals(seq + ":" + cookieValue)) {
session.setAttribute("seq:cookie", seq + ":" + cookieValue);
}
}
BoardVO vo = new BoardVO();
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
// 글 상세 조회
BoardVO board = boardDAO.getBoard(vo);
// 조회수 카운트
if (!session.getAttribute("seq:cookie").equals(session.getAttribute("seq:cookie ex"))) {
boardDAO.updateBoardCnt(vo);
// 가시적으로 조회수 1 추가해줌
board.setCnt(board.getCnt() + 1);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글 상세</title>
</head>
<body>
<center>
<h1>글 상세</h1>
<a href="logout_proc.jsp">log out</a>
<hr>
<form action="updateBoard_proc.jsp" method="post">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange" width="70">제목</td>
<td align="left">
<input name="title" type="text" value="<%= board.getTitle() %>">
</td>
</tr>
<tr>
<td bgcolor="orange">작성자</td>
<td align="left"><%= board.getWriter() %></td>
</tr>
<tr>
<td bgcolor="orange">내용</td>
<td align="left">
<textarea name="content" cols="40" rows="10"><%= board.getContent() %></textarea>
</td>
</tr>
<tr>
<td bgcolor="orange">등록일</td>
<td align="left"><%= board.getRegDate() %></td>
</tr>
<tr>
<td bgcolor="orange">조회수</td>
<td align="left"><%= board.getCnt() %></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="글 수정" />
</td>
</tr>
</table>
<input name="seq" type="hidden" value="<%= board.getSeq() %>" />
</form>
<hr>
<a href="insertBoard.jsp">글 등록</a>
<a href="deleteBoard_proc.jsp?seq=<%= board.getSeq() %>">글 삭제</a>
<a href="getBoardList.jsp">글 목록</a>
</center>
</body>
</html>
5. 글 삭제 기능 추가 : deleteBoard_proc.jsp
deleteBoard_proc.jsp
<%@ page import="com.springbook.biz.board.impl.BoardDAO" %>
<%@ page import="com.springbook.biz.board.BoardVO" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String seq = request.getParameter("seq");
BoardVO vo = new BoardVO();
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
boardDAO.deleteBoard(vo);
response.sendRedirect("getBoardList.jsp");
%>
'(2019) 사이드 프로젝트 > BoardWeb(게시판-MVC1,MVC2,스프링MVC)' 카테고리의 다른 글
8. MVC Model2 아키텍처로 게시판 개발 (0) | 2019.07.12 |
---|---|
7-7. 실습 중간점검 , 프로젝트 별첨 (0) | 2019.07.05 |
7-6. 페이징처리(paging, pagination) : BoardDAO.java, BoardList2.jsp, PagingVO.java (2) | 2019.07.04 |
7-5. 실습 중간점검 , 프로젝트 별첨 (0) | 2019.06.28 |
7-4. 글 조회수 카운트, session을 이용한 조회수 중복 방지 처리(jsp) : BoardDAO.java, getBoard.jsp (0) | 2019.06.28 |
7-3. 글 상세 조회 : getBoard.jsp (0) | 2019.06.24 |
7-2. 글 목록 조회, 글 검색 기능 : getBoardList.jsp, BoardDAO.java (0) | 2019.06.24 |
7-1. 로그인, 로그아웃, 로그인 검증 및 실패 후처리 : login.jsp, login_proc.jsp, logout_proc.jsp (3) | 2019.06.24 |