jspblog : update post

This commit is contained in:
kim
2021-01-22 15:03:58 +09:00
parent 3e31e38789
commit 094606e426
6 changed files with 105 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package com.example.jspblog.domain.board;
import com.example.jspblog.config.DB;
import com.example.jspblog.domain.board.dto.DetailResDto;
import com.example.jspblog.domain.board.dto.UpdateReqDto;
import com.example.jspblog.domain.board.dto.WriteReqDto;
import java.sql.Connection;
@@ -159,4 +160,26 @@ public class BoardDao {
}
return -1;
}
public int update(UpdateReqDto dto) {
String sql = "update board set title=?, content=? where id =?";
Connection conn = DB.getConnection();
PreparedStatement pstmt = null;
if (conn != null) {
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dto.getTitle());
pstmt.setString(2, dto.getContent());
pstmt.setInt(3, dto.getId());
int result = pstmt.executeUpdate();
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
DB.close(conn, pstmt);
}
}
return -1;
}
}

View File

@@ -0,0 +1,13 @@
package com.example.jspblog.domain.board.dto;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class UpdateReqDto {
private int id;
private String title;
private String content;
}

View File

@@ -3,6 +3,7 @@ package com.example.jspblog.service;
import com.example.jspblog.domain.board.Board;
import com.example.jspblog.domain.board.BoardDao;
import com.example.jspblog.domain.board.dto.DetailResDto;
import com.example.jspblog.domain.board.dto.UpdateReqDto;
import com.example.jspblog.domain.board.dto.WriteReqDto;
import java.util.List;
@@ -35,4 +36,8 @@ public class BoardService {
public int 글삭제(int id) {
return boardDao.deleteById(id);
}
public int 글수정(UpdateReqDto dto) {
return boardDao.update(dto);
}
}

View File

@@ -1,10 +1,7 @@
package com.example.jspblog.web;
import com.example.jspblog.domain.board.Board;
import com.example.jspblog.domain.board.dto.DeleteReqDto;
import com.example.jspblog.domain.board.dto.DeleteResDto;
import com.example.jspblog.domain.board.dto.DetailResDto;
import com.example.jspblog.domain.board.dto.WriteReqDto;
import com.example.jspblog.domain.board.dto.*;
import com.example.jspblog.domain.user.User;
import com.example.jspblog.service.BoardService;
import com.example.jspblog.service.UserService;
@@ -74,6 +71,7 @@ public class BoardController extends HttpServlet {
request.setAttribute("lastPage", lastPage);
request.setAttribute("currentPosition", currentPosition);
request.getRequestDispatcher("board/list.jsp").forward(request, response);
break;
}
case "detail" : {
int id = Integer.parseInt(request.getParameter("id"));
@@ -84,6 +82,7 @@ public class BoardController extends HttpServlet {
request.setAttribute("detail", dto);
request.getRequestDispatcher("board/detail.jsp").forward(request, response);
}
break;
}
case "delete" : {
BufferedReader br = request.getReader();
@@ -102,6 +101,32 @@ public class BoardController extends HttpServlet {
PrintWriter out = response.getWriter();
out.print(resData);
out.flush();
break;
}
case "updateForm" : {
int id = Integer.parseInt(request.getParameter("id"));
DetailResDto dto = boardService.글상세보기(id);
request.setAttribute("detail", dto);
request.getRequestDispatcher("board/updateForm.jsp").forward(request, response);
break;
}
case "update" : {
int id = Integer.parseInt(request.getParameter("id"));
String title = request.getParameter("title");
String content = request.getParameter("content");
UpdateReqDto dto = UpdateReqDto.builder()
.id(id)
.title(title)
.content(content)
.build();
int result = boardService.글수정(dto);
if (result == 1) {
response.sendRedirect("/jspblog/board?cmd=detail&id=" + id);
} else {
Script.back(response, "글 수정 실패");
}
}
}
}

View File

@@ -4,6 +4,7 @@
<div class="container">
<c:if test="${sessionScope.principal.id == detail.userId}">
<a href="${pageContext.request.contextPath}/board?cmd=updateForm&id=${detail.id}" class="btn btn-warning">수정</a>
<button onclick="deleteById(${detail.id})" class="btn btn-danger">삭제</button>
</c:if>

View File

@@ -0,0 +1,34 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../layout/header.jsp" %>
<div class="container">
<form action="${pageContext.request.contextPath}/board?cmd=update" method="POST">
<input type="hidden" name="id" value="${detail.id}">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" placeholder="title" id="title" name="title" value="${detail.title}">
</div>
<div class="form-group">
<label for="summernote">Content:</label>
<textarea class="form-control" rows="5" id="summernote" name="content">${detail.content}</textarea>
</div>
<button type="submit" class="btn btn-primary">글쓰기 수정</button>
</form>
</div>
<script>
$(document).ready(function() {
$('#summernote').summernote({
placeholder: '글쓰기',
tabsize: 2,
height: 400
});
});
</script>
</body>
</html>