64. spring으로 게시물 수정하기

hanahana·2022년 9월 13일
0

Spring 학원수강

목록 보기
18/45
post-thumbnail

Controller

수정페이지 연결 controller

/**
	 * 게시물 수정화면 연결
	 * @param mv
	 * @param boardNo
	 * @return
	 */
	@RequestMapping(value="/board/modifyView.kh", method = RequestMethod.GET)
	public ModelAndView boardModifyView(ModelAndView mv,
			@RequestParam("boardNo") Integer boardNo) {
		try {
		Board board = bService.printOneVyNo(boardNo);
		if(board!=null){
			
			mv.addObject("board",board);
			mv.setViewName("board/modifyForm");
		}
		
		}
		catch (Exception e) {
			e.printStackTrace();
			mv.addObject("mgs",e.getMessage());
			mv.setViewName("/common/errorPage");
		}
		
		return mv;
	}

/board/modifyView.kh 를 겟방식으로 접속하면 해당 폼이 나오도록 만들었다.

  • 상세게시물을 볼때 썼던 메소드를 활용하여 board값을 똑같이 가져와 수정페이지에서도 value로 사용하였다. bService.printOneVyNo(boardNo)

수정페이지로 연결하기

  • 수정페이지는 게시물 쓰기 페이지와 거의 같다.
<h1 align="center">${board.boardNo }번 게시물 수정하기</h1>
	<br>
	<br>
	
	
	<!-- enctype을 이용해 파일을 전송한다 오타조심!!!!-->
	<form action="/board/modify.kh" method="post"	enctype="multipart/form-data">
	<input type="hidden" name="boardNo" value="${board.boardNo }" readonly>
		<table align="center" border="1">
			<tr>
				<td>제목</td>
				<td><input type="text" name="boardTitle" value="${board.boardTitle }"></td>
			</tr>
			<tr>
				<td>작성자</td>
				<td><input type="text" name="boardWirter" value="${board.boardWirter }" readonly="readonly"></td>
			</tr>
			<tr>
				<td>내용</td>

				<td><textarea id="summernote" name="boardContents"
						style="resize: none; width: 500px; height: 100%;"> ${board.boardContents }</textarea></td>
			</tr>
			<tr>
				<td>첨부파일</td>
				<td><input type="file" name="uploadFile">
				<a href="#"> ${board.boardFile }</a>
				</td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="등록"> <input
					type="reset" value="취소">
					<a href="#">목록으로</a>
					<a href="#">이전페이지로</a>
					</td>
			</tr>

		</table>

	</form>

	<script>
		$('#summernote').summernote({
			height : 300, // set editor height
			minHeight : null, // set minimum height of editor
			maxHeight : null, // set maximum height of editor
			focus : true, // set focus to editable area after initializing summernote

		});
		$('[aria-label="Picture"]').css('display','none');
	</script>
  • 수정페이지이기에 이전 게시물의 값을 value로 가져왔음을 알수있다.
  • 첨부파일의 경우 아래에 일단 이름으로 연결해두었다.

수정Controller

/**
	 *게시물 수정코드*/
	@RequestMapping (value="/board/modify.kh", method = RequestMethod.POST)
	public ModelAndView modifyBoard(ModelAndView mv,
			@ModelAttribute Board board) {
		try {
		int result = bService.modifyBoardOneByNo(board);
		if(result>0) {System.out.println("성공");}else { System.out.println("실패");}
		mv.setViewName("redirect:/board/detail.kh?boardNo="+board.getBoardNo());
		
		}catch (Exception e) {
			e.printStackTrace();
			mv.addObject("mgs",e.getMessage());
			mv.setViewName("/common/errorPage");
		}
		return mv;
	}
  • jsp에서 폼에 입력한 값은 @ModelAttribute Board board를 통해 board로 저장되었다,
  • 서비스 클래스로 입력한 board값을 전송한다

Service Store Mapper

Service

@Override
	public int modifyBoardOneByNo(Board board) {
		int result = bStore.updateBoardOneByNo(board, session);
		return result;
	}
  • 저장한 board와 session값을 전달한다

Store

@Override
	public int updateBoardOneByNo(Board board, SqlSessionTemplate session) {
		int result = session.update("BoardMapper.modifyBoard",board);
		return result;
	}
  • 받은 값과 session을 이용해 Mapper에 update태그를 이용한다.

Mapper

<update id="modifyBoard">
update board_tbl set board_title = #{boardTitle}, BOARD_CONTENTS = #{boardContents} where BOARD_NO = #{boardNo}
</update>
  • 전송받은 값을 활용하여 쿼리문을 실행한다.

다시 Controller보기

/**
	 *게시물 수정코드*/
	@RequestMapping (value="/board/modify.kh", method = RequestMethod.POST)
	public ModelAndView modifyBoard(ModelAndView mv,
			@ModelAttribute Board board) {
		try {
		int result = bService.modifyBoardOneByNo(board);
				mv.setViewName("redirect:/board/detail.kh?boardNo="+board.getBoardNo());
		
		}catch (Exception e) {
			e.printStackTrace();
			mv.addObject("mgs",e.getMessage());
			mv.setViewName("/common/errorPage");
		}
		return mv;
	}
  • 수정페이지에 input hidden을 활용하여 boardNo를 저장했기에 boardNo를 활용하여 해당 게시물번호의 디테일페이지로 접속할수있다.

나머지는

  • 목록으로 넘기기, 첨부파일 바꾸기 등등은 나중에 계속…
profile
hello world

0개의 댓글