spring 게시판 구현 게시글 수정

박예린·2023년 2월 26일

게시판 게시글 수정

  1. 로그인 되어있는 session의 userid가 bbsdetail.jsp view에서의 작성자가 같다면
    -> bbsdetail.jsp view에 수정 버튼을 보여줌
  2. 수정버튼 클릭시 updatebbs.do로 이동
  3. 제목과 내용 모두 수정한 후 완료 버튼을 누르면 bbsupdateAf.do로 넘어감
  4. 새로 입력된 제목, 내용과 게시글의 seq를 받아와 Controller에서 dto에 담아줌

view

bbsdetail.jsp

<!-- 로그인 한 아이디와 글 작성한 아이디가 같다면 수정과 삭제를 보여줌 -->
<%
MemberDto login = (MemberDto)session.getAttribute("login");
if(dto.getId().equals(login.getId())){
	%>
	<button type="button" class="btn btn-primary" onclick="updateBbs(<%=dto.getSeq() %>)">수정</button>
	
	<button type="button" class="btn btn-primary" onclick="deleteBbs(<%=dto.getSeq() %>)">삭제</button>
	<%
}
%>
</div>

<script type="text/javascript">
function updateBbs( seq ) {
	location.href = "bbsupdate.do?seq=" + seq;
}
function deleteBbs( seq ) {
	location.href = "bbsdelete.do?seq=" + seq;  // update del=1
}
</script>

<table>
<col width="1500px"><col width="150px">
<tr>
	<td>comment</td>
	<td style="padding-left: 30px">올리기</td>
</tr>
<tr>
	<td>
		<textarea rows="3" class="form-control" name="content"></textarea>
	</td> 
	<td style="padding-left: 30px">
		<button type="submit" class="btn btn-primary btn-block p-4">완료</button>
	</td>
</tr>
</table>
</form>

<br><br>

<table class="table table-sm">
<col width="500"><col width="500">
<tbody id="tbody">
</tbody>
</table>
</script>

bbsupdate.jsp

<%@page import="mul.cam.a.dto.MemberDto"%>
<%@page import="mul.cam.a.dto.BbsDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
BbsDto dto = (BbsDto)request.getAttribute("dto");
System.out.println(dto.toString());
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

<style type="text/css">
th{
	background-color: #007bff;
	color: white;
	text-align: center;
	vertical-align: middle;
}
</style>
</head>
<body>

<h1>글수정</h1>

<hr>

<div id="app" class="container">

<form action="bbsupdateAf.do" id="frm" method="get">
<input type="hidden" name="seq" value="<%=dto.getSeq() %>" >

<table class="table table-sm">
<col width="100px"><col width="500px">

<tr>
	<th>아이디</th>
	<td>		
		<%=dto.getId() %>
		<input type="hidden" id="id" name="id" value="<%=dto.getId() %>">		
	</td>
</tr>
<tr>
	<th class="align-middle">제목</th>
	<td>
		<input type="text" id="title" name="title" size="50px" class="form-control form-control-lg" value='<%=dto.getTitle() %>'>
	</td>
</tr>
<tr>	
	<td colspan="2">
		<textarea rows="18" id="content" name="content" class="form-control"><%=dto.getContent()  %></textarea>
	</td>
</tr>
<tr>
	<td colspan="2" align="right" style="padding-top: 20px">
		<button type="button" class="btn btn-primary">글수정 완료</button>
	</td>
</tr>

</table>
</form>
</div>


<script type="text/javascript">
$(document).ready(function() {	
	$("button").click(function() {		
		if($("#title").val().trim() == "" ){
			alert("제목을 기입해 주십시오");
			return;
		}else if($("#content").val().trim() == "" ){
			alert("내용을 기입해 주십시오");
			return;
		}else{
			$("#frm").submit();
		}		
	});	
});
</script>
</body>
</html>

Controller

BbsController.java


	/* 수정 */
	@GetMapping(value = "bbsupdate.do")
	public String bbsupdate(Model model, int seq) {
		BbsDto dto = service.getBbs(seq);
		model.addAttribute("dto", dto);
		
		return "bbsupdate";
	}
	
	@GetMapping(value = "bbsupdateAf.do")
	public String bbsupdateAf(Model model, BbsDto dto) {
		System.out.println(dto.toString());
		boolean isS = service.updateBbs(dto);
		
		String bbsupdate = "BBS_UPDATE_OK";
		if(!isS) {			
			bbsupdate = "BBS_UPDATE_NG";
		}
		model.addAttribute("bbsupdate", bbsupdate);
		model.addAttribute("seq", dto.getSeq());
		
		return "message";
	}

mapper

Bbs.xml

<!-- update문 -->
<update id="bbsupdate" parameterType="mul.cam.a.dto.BbsDto">
	update bbs
	set step=step+1
	where ref=(select ref from bbs where seq=?)
		and step>(select step from bbs where seq=?)
</update>

DAO/Service

BbsDao.java

//수정하기
int updateBbs(BbsDto dto);

BbsDaoImpl.java

@Override
	public int updateBbs(BbsDto dto) {	
		return session.update(ns + "updateBbs", dto);
	}

BbsService.java

boolean updateBbs(BbsDto dto);

BbsServiceImpl.java

@Override
	public boolean updateBbs(BbsDto dto) {
		int n = dao.updateBbs(dto); 
		return n>0?true:false;
	}
profile
개발자를 꿈꾸는 귀여운 나

0개의 댓글