[스프링] UPDATE - 글 수정하기


JSP파일 만들기


/board/update.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<style>
	.center{
		margin: 5px 25px; padding: 20px
	}
</style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>update</title>
</head>
<body>
    <h2>Update Page</h2>

	<form name="update" method="post" action="${path}/board/update?num=${data.num}" class="center">	
        <div class="form-group">
            <label>이름</label>
            <input type="text" class="form-control" name="uName" value="${data.uName}">
        </div>
        <div class="form-group">
            <label>제목</label>
            <input type="text" class="form-control" name="subject" value="${data.subject}">
        </div>
        <div class="form-group">
            <label>내용</label>
            <input class="form-control" name="content" value="${data.content}">
        </div>
       
    <button type="submit" class="btn btn-outline-info">수정하기</button>
    <button type="reset" class="btn btn-outline-info">다시쓰기</button>
    <button type="button" class="btn btn-outline-info"><a href="/board/list">돌아가기</a></button>
    
    </form>
</body>
</html>

+

read.jsp 파일에 버튼 추가해주기

<a href="/board/update?num=${data.num}" role="button" class="btn btn-outline-info">수정하기</a>



Controller


// update.jsp 페이지로 이동
@RequestMapping(value="/update", method = RequestMethod.GET)
public String getUpdate() {
    return "board/update";
}



Controller 수정


// update.jsp 페이지로 이동
@RequestMapping(value="/update", method = RequestMethod.GET)
public String getUpdate(int num, Model model) {
    TestVO data = service.read(num);
    model.addAttribute("data", data);
    return "board/update";
}



DAO


  • DAO
public void update(TestVO vo);
  • DAOImpl
// update
@Override
public void update(TestVO vo) {
    sqlSession.update(namespace + ".update", vo);
    
}



Service


  • Service
public void update(TestVO vo);
  • ServiceImpl
@Override
public void update(TestVO vo) {
    dao.update(vo);
}



mapper


<!-- 게시글 수정 -->
<update id="update" parameterType="com.springtest.domain.TestVO">
    update springtest set
    uName = #{uName}, subject = #{subject}, content = #{content} where num = #{num}
</update>



Controller 추가


// update.jsp 게시글 수정하기
@RequestMapping(value="/update", method = RequestMethod.POST)
public String postUpdate(TestVO vo) throws Exception{
    service.update(vo);
    return "redirect:list";
}



실행하기


Categories:

Spring