공부하는 블로그

(웹개발) 수정 기능 추가하기 (Update) 본문

Develop/Spring

(웹개발) 수정 기능 추가하기 (Update)

모아&모지리 2017. 9. 29. 14:42
1
<a href="<c:url value='/board/update/${boardVO.boardId}' />">수정</a>
cs

read.jsp에 추가



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script type="text/javascript">
        function save(){
            var writeForm = document.getElementById("writeForm");
            writeForm.method = "post";
            <c:if test="${empty boardVO}">
            writeForm.action = "<c:url value='/board/doWrite'/>";
            </c:if>
            <c:if test="${not empty boardVO}">
            writeForm.action = "<c:url value='/board/doUpdate/${boardVO.boardId}'/>";
            </c:if>
            writeForm.submit();
        }
        function back() {
            <c:if test="${empty boardVO}">
            location.href = "<c:url value='/board/list'/>";
            </c:if>
            <c:if test="${not empty boardVO}">
            location.href = "<c:url value='/board/read/${boardVO.boardId}'/>";
            </c:if>
        }
cs


<c:if test="${empty boardVO}"> 값이 있으면 없으면 나눠준다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@RequestMapping("/board/update/{boardId}")
    public ModelAndView viewUpdatePage(@PathVariable int boardId, HttpSession session) {
        
        MemberVO member = (MemberVO) session.getAttribute("_USER_");
        String id = member.getId();
        
        BoardVO boardVO = boardService.readOneBoardVO(boardId);
        
        if ( boardVO.getId().equals(id)) {  // 내글일때
            
            ModelAndView view = new ModelAndView();
            view.setViewName("board/write");
            
            view.addObject("boardVO", boardVO);  // 글보여주기
            return view;
            
        }
        else {                                 //내 글이 아닐때 
            return new ModelAndView("redirect:/board/read" + boardId);
        }
    }
cs



controller에 RequestMapping 추가


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div class="btns">
            <a href="javascript:void(0);" 
            class="registBtn" 
            onclick="save();">
            <c:if test="${empty boardVO}">
            등록
            </c:if>
            <c:if test="${not empty boardVO}">
            수정
            </c:if>
            </a>
            
            <div>
                <a href="javascript:void(0);" class="cancelBtn" 
                onclick="back();">돌아가기</a>
            </div>
        </div>
cs


버튼도 바꿔준다.

back() 자바스크립트는 위에서 바꿔준다.




BoardDao 추가


BoardDaoImpe 추가


1
2
3
4
5
6
7
8
    <update id ="updateBoard" parameterType="BoardVO">
      
          UPDATE    BOARD
          SET        SUBJECT = #{subject}
                  , CONTENT = #{content}
          WHERE     BOARD_ID = #{boardId}
          AND        ID = #{id}    
      </update>
cs


SQL추가


BoardService 추가

BoardServiceImpl 추가


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RequestMapping("/board/doUpdate/{boardId}")
    public String doUpdate(
            @PathVariable int boardId, BoardVO boardVO, HttpSession session) {
        
        MemberVO member = (MemberVO) session.getAttribute("_USER_");
        String id = member.getId();
        
        BoardVO oldBoardVO = boardService.readOneBoardVO(boardId);
        
        if ( oldBoardVO.getId().equals(id) ) {
            boardVO.setBoardId(boardId);
            boardVO.setId(id);
            boolean isSuccess = boardService.updateBoard(boardVO);
                    
        }
        return ("redirect:/board/read/" + boardId);
    }
cs


Mapping 추가