JAVA/SPRING

[SPRING] SPRING 게시판 (8) - CRUD [게시글 수정]

KMSEOP 2020. 3. 18. 15:42
728x90

Mapper 작성

<!-- mappers/BoardMapper.xml -->

...

	<!-- 추가 -->
	<update id="update">
		UPDATE board
		SET title=#{title}, content=#{content}, editTime=now()
		WHERE bno=#{bno}
	</update>

 

Dao 작성

// Dao/BoardDao.java

...

	//추가
	public void updatePosts(BoardVo posts) throws Exception{
		
		sqlSession.update(Namespace + ".update", posts);
	}

 

테스트케이스 작성

// test/boardDaoTest.java

...

	//추가
	@Test
	public void testUpdatePosts() throws Exception{
		
		BoardVo posts = new BoardVo();
		posts.setBno(1);
		posts.setTitle("test");
		posts.setContent("contents");
		
		boardDao.updatePosts(posts);
	}

 

Service 작성

// Service/BoardService.java

...

	//추가
	public void updatePosts(BoardVo posts) throws Exception{
		
		boardDao.updatePosts(posts);
	}

 

Controller 작성

// Controller/BoardController.java

...


	//추가
	//게시글 수정
	@RequestMapping(value="/posts/{bno}", method=RequestMethod.GET)
	public ModelAndView update(@PathVariable int bno) throws Exception{
		
		BoardVo posts = boardService.detailPosts(bno);
		
		ModelAndView model = new ModelAndView();
		model.setViewName("update");
		model.addObject("posts", posts);
		
		return model;
	}
	
	//게시글 수정 처리
	@RequestMapping(value="/posts/{bno}", method=RequestMethod.PUT)
	public String updatePosts(@PathVariable int bno, BoardVo posts) throws Exception{
		
		boardService.updatePosts(posts);
		
		return "redirect:/" + bno;
	}

 

View 작성

<!-- views/detail.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <style>
      #list{
        border: 1px solid black;
      }
      th, td {
        border: 1px solid #444444;
      }
    </style>
  </head>
  <body>
    <table id="list">
      <thead>
        <tr>
          <th>NO</th>
          <th>TITLE</th>
          <th>WRITER</th>
          <th>COUNT</th>
          <th>DATE</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>${post.bno}</td>
          <td>${post.title}</td>
          <td>${post.writer}</td>
          <td>${post.count}</td>
          <td>${post.createTime}</td>
        </tr>
      </tbody>
    </table>
    <form action="/board/posts/${post.bno}" method="POST">
      <input type="hidden" name="_method" value="DELETE" />
      <input type="submit" value="DELETE">
    </form>
    <button type="button" id="update">UPDATE</button>
  </body>
</html>

<!-- Jquery -->
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script>
  //게시글 수정 페이지 이동
  $('#update').click(function(){
    location.href="/board/posts/${post.bno}";
  })
</script>
<!-- views/update.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form action="${posts.bno}" method="POST">
      <input type="hidden" name="_method" value="PUT"/>
      <input type="hidden" name="bno" value="${posts.bno}" />
      <table>
        <tr>
          <td><label>Title</label></td>
          <td><input type="text" name="title" value="${posts.title}" /></td>
        </tr>
        <tr>
          <td><label>Content</label></td>
          <td><input type="text" name="content" value="${posts.content}" /></td>
        </tr>
        <tr>
          <td> <button type="submit">WRITE</button> </td>
        </tr>
      </table>
    </form>
  </body>
</html>

<!-- Jquery -->
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>

 

728x90