68. 댓글 수정하기

hanahana·2022년 9월 19일
0

Spring 학원수강

목록 보기
30/45
post-thumbnail
  • 댓글수정은 jsp에 많이의존했다.

게시판 상세보기 JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${board.boardTitle }</title>

</head>
<body>
	<header>
		<jsp:include page="../home.jsp" />
	</header>

	<table align="center" border="1" width="500">
		<tr>
			<td>제목</td>
			<td>${board.boardTitle }</td>
		</tr>
		<tr>
			<td>작성자</td>
			<td>${board.boardWirter }</td>

		</tr>
		<tr>
			<td>작성일</td>
			<td>${board.bCreateDate }</td>
		</tr>
		<tr>
			<td>내용</td>

			<td>${board.boardContents }<br> <c:if
					test="${board.boardRename ne null}">
					<img alt="본문이미지"
						src="/resources/buploadFiles/${board.boardRename }">

				</c:if>
			</td>
		</tr>

		<tr>
			<td colspan="2">
				<!--검색/일반의 경우 목록주소 --> <!-- 일반으로 상세 진입시 목록 --> <c:if
					test="${empty searchValue  }">
					<input type="button" value="수정"
						onclick="location.href='/board/modifyView.kh?boardNo=${board.boardNo}&page=${pageNow }';">
					<button type="button"
						onclick="location.href='/board/list.kh?page=${pageNow }';">목록</button>
				</c:if> <!-- 검색 으로 상세 진입시 --> <c:if test="${searchValue ne null }">
					<input type="button" value="수정"
						onclick="location.href='/board/modifyView.kh?boardNo=${board.boardNo}&searchCondition=${searchCondition }&searchValue=${searchValue}&page=${pageNow }';">
					<button type="button"
						onclick="location.href='/board/search.kh?searchCondition=${searchCondition }&searchValue=${searchValue}&page=${pageNow }';">목록</button>
				</c:if> <!-- 목록주소 태그 종료 -->

				<button type="button" onclick="remove()">삭제</button>
			</td>
		</tr>

	</table>

	<!--  댓글 등록 -->

	<form action="/board/addReply.kh" method="post">
		<input type="hidden" name="refBoardNo" value="${board.boardNo }">
		<table align="center" width="500" border="1">
			<tr>
				<td><textarea rows="3" cols="55" placeholder="내용을 작성하세요"
						name="replyContents" required="required"></textarea></td>
				<td>
					<button>등록하기</button>
				</td>
			</tr>

		</table>
	</form>

<!--댓글목록/수정창-->
	<table align="center" width="500" border="1">

		<c:forEach items="${rList }" var="reply" varStatus="i">
			<tr>
				<td width="100">${reply.replyWirter }</td>
				<td>${reply.replyContents }</td>
				<td <c:if test="${reply.replyWirter != loginUser.memberId }"> colspan ="2"</c:if> >${reply.rCreateDate }</td>
				
				
				<c:if test="${reply.replyWirter == loginUser.memberId }">				
				<td><button type="button" onclick="modifyView(this.id)"
						id="modify${i.count }">수정</button> <a href="#">삭제</a> </td>
				</c:if>
				
			</tr>
<!-- 댓글 수정창-->
			<tr class="modifyViews">
				<form action="/board/modifireply.do" method="post">
					<td colspan="3"><input type="hidden" name="replyNo"
						value="${reply.replyNo }"> <input type="hidden"
						name="refBoardNo" value="${board.boardNo }">
						<input type="hidden" name="replyWirter"
						value="${reply.replyWirter }">
						 <textarea
							rows="3" cols="55" placeholder="내용을 작성하세요" name="replyContents"
							required="required" id="${reply.replyNo }">
							${reply.replyContents }
					</textarea></td>
					<td>
						<button>수정</button>
						<button onclick="modifyCancle('modify${i.count }')" type="button">취소</button>
					</td>
				</form>
			</tr>
		</c:forEach>
	</table>

	</form>

	<script>
	
	
		var modifyViews = document.getElementsByClassName('modifyViews')
		for (var i = 0; i < modifyViews.length; i++) {
			modifyViews[i].style.display = 'none'
		}
		function modifyCancle(idI) {
			var modifiId = document.getElementById(idI);
			modifiId.parentElement.parentElement.style.display = '';
			modifiId.parentElement.parentElement.nextElementSibling.style.display = 'none';
		}
		function modifyView(idI) {
			var modifiId = document.getElementById(idI);
			modifiId.parentElement.parentElement.style.display = 'none';
			modifiId.parentElement.parentElement.nextElementSibling.style.display = '';
		}
		function remove() {
			event.preventDefault(); // 하이퍼링크 이동방지
			if (window.confirm("게시물을 삭제하시겠습니까?")) {
				location.href = '/board/remove.kh?page=${pageNow}';
			}
		}
		
		

	</script>

</body>
</html>
  • 댓글 목록창 : c:if문을 활용해 현재 로그인한 아이디와 작성한 id가 같지 않으면 수정/삭제가 보이지 않도록 하였다.
    <!--댓글목록/수정창-->
    	<table align="center" width="500" border="1">
    
    		<c:forEach items="${rList }" var="reply" varStatus="i">
    			<tr>
    				<td width="100">${reply.replyWirter }</td>
    				<td>${reply.replyContents }</td>
    				<td <c:if test="${reply.replyWirter != loginUser.memberId }"> colspan ="2"</c:if> >${reply.rCreateDate }</td>
    				<!-- id와 작성자가 다르면 td는 2칸을 차지한다-->
    				
    				<!-- id와 작성자가 같을때만 수정/삭제가 보인다-->
    				<c:if test="${reply.replyWirter == loginUser.memberId }">				
    				<td><button type="button" onclick="modifyView(this.id)"
    						id="modify${i.count }">수정</button> <a href="#">삭제</a> </td>
    				</c:if>

Cotroller

/**
	 * 댓글수정
	 * @param mv
	 * @param reply
	 * @return
	 */
	@RequestMapping(value="/board/modifireply.do", method=RequestMethod.POST)
	public ModelAndView modifyReply(ModelAndView mv, @ModelAttribute Reply reply, HttpSession session) {
		Member member = (Member)session.getAttribute("loginUser");
	
		if(!reply.getReplyWirter().equals((member.getMemberId()))) {
			mv.addObject("msg","작성자가 아니면 수정할수 없습니다.");
			mv.setViewName("/common/errorPage");
			return mv;
		}
		
		try {
		int result = bService.modifyReply(reply);
		int boardNo = reply.getRefBoardNo();
		mv.setViewName("redirect:/board/detail.kh?boardNo="+boardNo);
		}catch (Exception e) {
			mv.addObject("msg",e.getMessage());
			mv.setViewName("/common/errorPage");
		}
		return mv;
	}
  • 컨트롤러 창에서도 수정자와 작성자가 다를경우 오류페이지로 전송하도록했다.
  • 나머지는 댓글 등록과 크게 다르지 않다.

Service Store Mapper

Service

@Override
	public int modifyReply(Reply reply) {
		int result =bStore.updateOneReply(reply, session);
		return result;
	}

Store

@Override
	public int updateOneReply(Reply reply, SqlSessionTemplate session) {
		int result = session.update("BoardMapper.updateOneReply", reply);
		return result;
	}

Mapper

<update id="updateOneReply">
	update reply_tbl set reply_contents = #{replyContents}, R_UPDATE_DATE = default where reply_no = #{replyNo}
	</update>
profile
hello world

0개의 댓글