💡 핵심 요약 (TL;DR)
스프링게시판5: 글 수정, 글 삭제의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* console.log()가 alert()보다 더 많은 정보를 출력해준다. 개발시에는 console.log()를 더 많이 사용하자.
BoardController.java 코드 추가
@SessionAttributes() : 컨트롤러의 정보를 저장하는 어노테이션
기존 Session객체와 다른 점 : 서버단 전체에서 저장한 정보를 이용할 수 있다. 반면 @SessionAttributes()은 해당 컨트롤러안에서만 저장한 데이터를 이용할 수 있다.
예를 들어 제목이 not null제약조건이지만 공백이 있는 경우 저장된다. 잘못된 정보이므로 이전 정보로 되돌리고 싶을때 @SessionAttributes("boardVO")로 되돌릴 수 가 있다.
@SessionAttributes("boardVO") : boardVO의 정보를 세션으로 저장하고 다닌다.
@SessionAttributes, SessionStatus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 @RequestMapping(value = "/modify", method = RequestMethod.GET) public void modifyGET (@RequestParam("bno") int bno, Model model) throws Exception{ l.info("C: modify 겟 메서드 파라미터 : " +bno); model.addAttribute("bvo" , service.readContent(bno)); } @RequestMapping(value = "/modify", method = RequestMethod.POST) public String modifyPOST (BoardVO vo,RedirectAttributes rttr) throws Exception{ l.info("C: modify 포스트 메서드 파라미터 : " +vo); service.modify(vo); rttr.addFlashAttribute("result" , "up-ok" ); return "redirect:/board/listAll" ; } @RequestMapping(value = "/remove", method = RequestMethod.POST) public String removePOST (@RequestParam("bno") int bno,RedirectAttributes rttr) throws Exception{ l.info("C: remove 포스트 메서드" ); service.remove(bno); rttr.addFlashAttribute("result" , "delete-ok" ); return "redirect:/board/listAll" ; }
read.jsp 뷰 버튼 수정1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <button type ="submit" class ="btn btn-warning" > 글 수정</button > <button type ="submit" class ="btn btn-danger" > 글 삭제</button > <button type ="submit" class ="btn btn-primary" > 글 목록으로</button > //스크립트 <script type ="text/javascript" > $(document ).ready (function ( ){ let formObj = $("form[role='form']" ); $(".btn-primary" ).on ("click" , function ( ){ <!-- 글 번호 가져가지고 페이지이동 첫번째 방법 --> location.href ="/board/listAll" ; }); $(".btn-warning" ).click (function ( ){ formObj.attr ({ action : "/board/modify" , method : "get" , }).submit (); }); $(".btn-danger" ).click (function ( ){ formObj.attr ("action" ,"/board/remove" ).submit (); }); }) </script >
modify.jsp 뷰 연결
console.log()가 alert()보다 더 많은 정보를 출력해준다. 개발시에는 console.log()를 더 많이 사용하자.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="../include/header.jsp" %> <section class ="content" > <div class ="row" > <div class ="col-md-12" > <div class ="box box-primary" > <div class ="box-header" > <h3 class ="box-title" > ITWILL 게시글 상세페이지</h3 > </div > <form method ="post" role ="form" action ="" > <input type ="hidden" name ="bno" value ="${bvo.bno }" > <div class ="box-body" > <div > <span class ="form-group" > <label for ="exampleInputEmail" > 글번호</label > <input type ="text" name ="bno" class ="form-control" style ="width:200px;display:inline;margin-right:100px;" value ="${bvo.bno}" readonly > </span > <span class ="form-group" > <label for ="exampleInputEmail" > 글쓴이</label > <input type ="text" name ="writer" class ="form-control" style ="width:200px;display:inline;margin-right:100px;" value ="${bvo.writer}" readonly > </span > <span class ="form-group" > <label for ="exampleInputEmail" > 작성일자</label > <input type ="text" name ="regdate" class ="form-control" style ="width:200px;display:inline;margin-right:100px;" value ="${bvo.regdate}" readonly > </span > </div > <div class ="form-group" > <label for ="exampleInputEmail" > 제목</label > <input type ="text" name ="title" class ="form-control" value ="${bvo.title}" > </div > <div class ="form-group" > <label for ="exampleInputEmail" > 내용</label > <textarea name ="content" rows ="5" class ="form-control" > ${bvo.content}</textarea > </div > </div > </form > <div class ="box-footer" > <button type ="submit" class ="btn btn-warning" > 글 수정</button > <button type ="submit" class ="btn btn-primary" > 글 목록으로</button > </div > </div > </div > </div > </section > </div > <script type ="text/javascript" > $(document ).ready (function ( ){ let formObj = $("form[role='form']" ); console .log (formObj); $(".btn-primary" ).on ("click" , function ( ){ location.href ="/board/listAll" ; }); $(".btn-warning" ).click (function ( ){ formObj.submit (); }); }) </script > <%@ include file="../include/footer.jsp" %>
listAll.jsp 코드 추가
얼럿창 구현코드를 스크립트 태그안에 구현해준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <script type="text/javascript"> let result="${result}"; let isResist = "${isRegist}"; if(result == 'success' && isResist == 'true'){ alert('성공적으로 글 작성되었습니다.'); }else if(result == 'success' && isResist != 'true'){ alert('글쓰기가 실패하였습니다.'); }else if(result =='up-ok'){ alert('성공적으로 글이 수정되었습니다.'); }else if(result =='delete-ok'){ alert('성공적으로 글이 삭제되었습니다.'); }else{ //글쓰기없이 출력시 alert창 필요없음 } </script>
BoardService.java 인터페이스에 메서드 추가1 2 3 4 5 public void modify (BoardVO vo) throws Exception;public void remove (Integer bno) throws Exception;
BoardServiceImpl.java 메서드 오버라이딩 코드 추가1 2 3 4 5 6 7 8 9 10 11 @Override public void modify (BoardVO vo) throws Exception { bdao.modify(vo); } @Override public void remove (Integer bno) throws Exception { bdao.delete(bno); }
BoardDAO.java 인터페이스에 메서드 추가1 2 3 4 5 public void modify (BoardVO vo) throws Exception;public void delete (Integer bno) throws Exception;
BoardDAOImpl.java 메서드 오버라이딩 코드 추가1 2 3 4 5 6 7 8 9 10 11 12 @Override public void modify (BoardVO vo) throws Exception { System.out.println("DAO: bno는?" +vo.getBno()); session.update(namespace+".modify" , vo); } @Override public void delete (Integer bno) throws Exception { session.delete(namespace+".delete" , bno); }
BoardMapper.xml SQL쿼리 추가1 2 3 4 5 6 7 8 9 10 <update id ="modify" > update tbl_board set title=#{title},content=#{content} where bno=#{bno} </update > <delete id ="delete" > delete from tbl_board where bno=#{bno} </delete >
#TIL ITWILL(JAVA)