728x90

수정하기 기능을 구현하겠다.

우선 update.jsp를 jstl form 태그를 이용하도록 수정한다.

                        	<form:form action="${root }board/update_pro" method="post" modelAttribute="updateContentBean" enctype="multipart/form-data">
                        		<form:hidden path="content_idx"/>
                        		<form:hidden path="content_board_idx"/>
                                <div class="form-group">
                                	<form:label path="content_writer_name">작성자</form:label>
                                	<form:input path="content_writer_name" class="form-control" readonly="true"/>
                                </div>
                                <div class="form-group">
                                	<form:label path="content_date">작성날짜</form:label>
                                	<form:input path="content_date" class="form-control" readonly="true"/>
                                </div>
                                <div class="form-group">
                                    <form:label path="content_subject">제목</form:label>
                                    <form:input path="content_subject" class="form-control"/>
                                    <form:errors path="content_subject" style="color:red"/>
                                </div>
                                <div class="form-group">
                                    <form:label path="content_text">내용</form:label>
                                    <form:textarea path="content_text" class="form-control" rows="10" style="resize:none"/>
                                    <form:errors path="content_text" style="color:red"/>
                                </div>
                                <div class="form-group">
                                	<form:label path="content_file">첨부 이미지</form:label>
                                	<c:if test="${updateContentBean.content_file != null }">
                                		<img src="${root }upload/${updateContentBean.content_file}" width="100%"/>
                                	</c:if>
                                	<form:input path="upload_file" type="file" class="form-control" accept="image/*"/>
                                </div>
                                <div class="form-group">
                                    <div class="text-right">
                                        <form:button class="btn btn-primary">수정완료</form:button>
                                        <a href="${root }board/read?board_info_idx=${board_info_idx}&content_idx=${content_idx}" class="btn btn-info">취소</a>
                                    </div>
                                </div>
                            </form:form>

controller의 update도 적절히 수정한다.

	@GetMapping("/board/update")
	public String update(@RequestParam("board_info_idx") int board_info_idx,
						 @RequestParam("content_idx") int content_idx,
						 @ModelAttribute("updateContentBean") ContentBean updateContentBean,
						 Model model) {
		
		model.addAttribute("board_info_idx", board_info_idx);
		model.addAttribute("content_idx", content_idx);
		
		return "board/update";
	}

아직 수정하기를 누르면 이전에 쓴 글의 정보가 뜨진 않는다.

정보를 보이게 하기 위해 Controller를 수정을 한다.

아래 코드를 추가하자. boardService에서 content_idx를 통해 글의 정보를 모두 가져온 후 updateContentBean에 정보를 update한다.

		ContentBean tempContentBean = boardService.getContent(content_idx);
		updateContentBean.setContent_writer_name(tempContentBean.getContent_writer_name());
		updateContentBean.setContent_date(tempContentBean.getContent_date());
		updateContentBean.setContent_subject(tempContentBean.getContent_subject());
		updateContentBean.setContent_text(tempContentBean.getContent_text());
		updateContentBean.setContent_idx(tempContentBean.getContent_idx());
		updateContentBean.setContent_board_idx(tempContentBean.getContent_board_idx());
		updateContentBean.setContent_file(tempContentBean.getContent_file());
		updateContentBean.setContent_writer_idx(tempContentBean.getContent_writer_idx());

고친 후 실행하면 아래와같이 정보가 잘 나타난다.

 


update처리를 하기위해 controller에 update_pro를 작성하고,

	@PostMapping("/board/update_pro")
	public String update_pro(@Valid @ModelAttribute("updateContentBean") ContentBean updateContentBean, BindingResult result) {
		if(result.hasErrors()) {
			return "board/update";
		}
		return "board/update_success";
	}

error_message도 세팅하자.

NotBlank.updateContentBean.content_subject = 제목을 입력해주세요.
NotBlank.updateContentBean.content_text = 내용을 입력해주세요. 

아래와 같이 수정하면 오류 메세지가 잘 나타남을 알 수 있다.

이때 문제점으로 첨부 이미지가 사라지는 게 있다.

content_file을 담는 form태그가 없기 때문에 form:hidden을 통해 content_file 이름을 유지하게 한다.

                                <div class="form-group">
                                	<form:label path="content_file">첨부 이미지</form:label>
                                	<c:if test="${updateContentBean.content_file != null }">
                                		<img src="${root }upload/${updateContentBean.content_file}" width="100%"/>
                                		<form:hidden path="content_file"/>
                                	</c:if>
                                	<form:input path="upload_file" type="file" class="form-control" accept="image/*"/>
                                </div>

이제 수정된 내용으로 게시글을 변경하는 작업을 하자.

BoardMapper로 간다.

	@Update("update content_table " + 
			"set content_subject=#{content_subject}, content_text=#{content_text}, " +
			"content_file=#{content_file,jdbcType=VARCHAR} " + 
			"where content_idx=#{content_idx}")
	void updateContent(ContentBean updateContentBean);

쿼리를 작성한 후 boardDao, boardService에서 호출한다.

boardService는 파일을 변경하는 작업까지 해야 한다.

	public void updateContent(ContentBean updateContentBean) {
		
		MultipartFile upload_file = updateContentBean.getUpload_file();
		if(upload_file.getSize()>0) {
			String file_name = saveUploadFile(upload_file);
			updateContentBean.setContent_file(file_name);
		}
		
		boardDao.updateContent(updateContentBean);
	}

controller도 수정하자.

	@GetMapping("/board/update")
	public String update(@RequestParam("board_info_idx") int board_info_idx,
						 @RequestParam("content_idx") int content_idx,
						 @ModelAttribute("updateContentBean") ContentBean updateContentBean,
						 Model model) {
		
		model.addAttribute("board_info_idx", board_info_idx);
		model.addAttribute("content_idx", content_idx);
		
		ContentBean tempContentBean = boardService.getContent(content_idx);
		updateContentBean.setContent_writer_name(tempContentBean.getContent_writer_name());
		updateContentBean.setContent_date(tempContentBean.getContent_date());
		updateContentBean.setContent_subject(tempContentBean.getContent_subject());
		updateContentBean.setContent_text(tempContentBean.getContent_text());
		updateContentBean.setContent_file(tempContentBean.getContent_file());
		updateContentBean.setContent_writer_idx(tempContentBean.getContent_writer_idx());
		updateContentBean.setContent_idx(content_idx);
		updateContentBean.setContent_board_idx(board_info_idx);
		
		return "board/update";
	}
	
	@PostMapping("/board/update_pro")
	public String update_pro(@Valid @ModelAttribute("updateContentBean") ContentBean updateContentBean, BindingResult result) {
		if(result.hasErrors()) {
			return "board/update";
		}
		
		boardService.updateContent(updateContentBean);
		
		return "board/update_success";
	}

이제 update_success.jsp까지 작성해주자.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var='root' value="${pageContext.request.contextPath}/"/>
<script>
	alert("수정되었습니다.")
	location.href = "${root}board/read?board_info_idx=${updateContentBean.content_board_idx}&content_idx=${updateContentBean.content_idx}"
</script>

 

이제 실행한 후 수정을 하면 아래와 같이 적절하게 수정됨을 알 수 있다.

728x90

+ Recent posts