JSP/BBS

view

GAWON 2023. 6. 9. 17:57
<%@page import="org.joonzis.ex.CVO"%>
<%@page import="java.util.List"%>
<%@page import="org.joonzis.ex.BDao"%>
<%@page import="org.joonzis.ex.BVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   request.setCharacterEncoding("utf-8");

   String b_idx = request.getParameter("b_idx");
   String currentPage = request.getParameter("currentPage");
   
   // b_idx를 이용해서 데이터 가져오기
   // BDao.getBBS 
   BVO bvo = BDao.getBBS(Integer.parseInt(b_idx));
   
   // 수정, 삭제를 위해서 session에 저장
   session.setAttribute("bvo", bvo);
   pageContext.setAttribute("currentPage", currentPage);   // JS에서 사용하려고 속성에 담음
   
   //------조회 수 처리-------
   /*
   	처음으로 view 를 열었을 때에만 조회수가 증가
   	게시물을 열 때 session에 open이라는 값을 저장
   	session에 open 값이 있으면 조회수를 증가시키지 않고 , open 값이 없을 때에만 조회수 증가
   */
   String open =(String)session.getAttribute("open");
   if(open == null){
	   //open이 null이면 해당 게시물을 처음 열었다는 의미
	   session.setAttribute("open", "yes");
	   int hit = bvo.getHit() + 1;
	   bvo.setHit(hit);
	   BDao.getUpdateHit(bvo);
   }
   
   //-----------댓글 리스트--------
   
	List<CVO> cList = BDao.getCommentList(Integer.parseInt(b_idx));
	pageContext.setAttribute("cList", cList);
   
	
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
	div {
		width: 800px;
		margin: auto;
		text-align: center;
	}
	table {
		width: 800px;
		text-align: left;
		border-collapse: collapse;		
	}
	
	td,th {
		border : 1px solid #1e90ff;
		padding: 10px;	
	}
	
	
	th {
		width: 130px;
		text-align: center;
		background-color: #0078ff;
		color: white;
	}
	
	#btn {
	text-align: center;
	}
	
	h1 {
		color : #0078ff;	
	}
		
	.insertComment th {
		border : 1px solid #1e90ff;
		width: 130px;
		text-align: center;
		background-color: #00a5ff;
		color: white;
	}

	.viewComment th {
		border : 1px solid #1e90ff;
		width: 130px;
		text-align: center;
		background-color: #87cefa;
		color: white;
	}
	.viewComment td{
		text-align: center;
	} 
	
	.viewComment #num  {
		width:50px;
	}
	.viewComment #writer  {
		width:100px;
	}
	.viewComment #con  {
		width:250px;
	}
	.viewComment #date  {
		width:100px;
	}
	.viewComment #del  {
		width:50px;
	}
	textarea {
		resize: none;
	}
</style>
<script type="text/javascript">
	function update_page(f){
		location.href='update_page.jsp';
	}
	function remove_page(f){
		location.href='remove_page.jsp';
	}
	function view_all(f){
		var cp = '${currentPage}';
		location.href='index.jsp?currentPage=' + cp;
	}
	function insert_comment(f){
		if(f.writer.value==''){
			alwer("작성자를 입력하세요");
			return;
		}
		f.action = 'insert_comment.jsp';
		f.submit();
	}
	function remove_comment(c_idx,b_idx,cp){
		location.href='remove_comment.jsp?c_idx='+c_idx+'&b_idx='+b_idx+'&currentPage='+cp;
	}
	

</script>
</head>
<body>
	<div>
		<h1>${bvo.b_idx } 번 게시글</h1>
		<form method="post">
			<table class="bbs">
				<tbody>
					<tr>
						<th>작성자</th>
						<td>${bvo.writer }</td>
						<th>IP</th>
						<td>${bvo.ip}</td>
					</tr>
					<tr>
						<th>제목</th>
						<td colspan="3">${bvo.title }</td>
					</tr>
					<tr>
						<th>첨부파일</th>
						<td colspan="3">
							<c:choose>
								<c:when test="${bvo.filename eq null }">
									첨부파일 없음
								</c:when>
								<c:otherwise>
									 <a href="download.jsp?path=upload&filename=${bvo.filename }">${bvo.filename }</a>
								</c:otherwise>
							</c:choose>
						</td>
					</tr>
					<tr>
						<th>내용</th>
						<td colspan="3">${bvo.content }</td>
					</tr>
					<tr>
						<td colspan="4" id="btn">
							<input type="button" value="게시글 수정하기" onclick="update_page()">
							<input type="button" value="게시글 삭제하기" onclick="remove_page()">
							<input type="button" value="목록으로 이동" onclick="view_all()">
							
						</td>
					</tr>
				</tbody>
			</table>
		</form>
		<br><hr><br>
		<!-- 댓글 입력 폼 -->
		<form method="post">
			<table>
				<tbody>
					<tr>
						<th>댓글 작성자</th>
						<td><input type="text" name="writer"></td>
						<th>댓글 비밀번호</th>
						<td><input type="password" name="pw"></td>
					</tr>
					<tr>
						<th>댓글 내용</th>
						<td colspan="3">
							<textarea rows="3" cols="80" name="content"></textarea>
						</td>
					</tr>
					<tr>
						<td colspan="4" id="btn">
							<input type="button" value="댓글 등록" onclick="insert_comment(this.form)">	
							<input type="reset" value="다시작성">
						</td>
					</tr>				
				</tbody>
			</table>
			<input type="hidden" name="b_idx" value="${bvo.b_idx}">
			<input type="hidden" name="currentPage" value="${currentPage}">
		</form>
		
		<br><hr><br>
		<!-- 댓글 출력 폼 -->
		<form method="post">
			<table>
				<thead>
					<tr>
						<th id="num">번호</th>
						<th id="writer">작성자</th>
						<th id="con">내용</th>
						<th id="date">작성일</th>
						<th id="del">삭제</th>
					</tr>	
				</thead>
				<tbody>
					<c:choose>
						<c:when test="${not empty cList }">
							<c:forEach var="vo" items="${cList }">
								<tr>
									<td>${vo.c_idx }</td>
									<td>${vo.writer}</td>
									<td>${vo.content }</td>
									<td>${vo.reg_date }</td>
									<td><a href="javascript:remove_comment(${vo.c_idx },${vo.b_idx },<%= currentPage %>)">삭제</a></td>
								</tr>
							</c:forEach>
						</c:when>
						<c:otherwise>
							<tr>
								<td colspan="5">댓글이 없습니다.</td>
							</tr>	
						</c:otherwise>
					</c:choose>
				</tbody>	
			</table>
		</form>
	</div>
</body>
</html>