위로
아래
글 수정 HTML
글쓰기 jsp와 똑같이 하되, 게시글 보기처럼 모든 데이터를 불러와 input의 value에 적어놓는다.
수정할 수 있도록 readonly는 제거한다.
css와 javaScript도 form을 적용한다.
<%@ 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="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글쓰기 폼</title>
<link rel="stylesheet" href="css/form.css" type="text/css">
<script src="jquery/jquery-3.7.0.min.js"></script>
<script src="js/form.js"></script>
</head>
<body>
<form name="form1" action="updateAction" method="post">
<table>
<tr><td colspan="2"><h3>게시글 수정</h3></td></tr>
<tr>
<th>제목</th>
<td><input type="text" name="subject" title="제목" class="chk" value="${article.subject}"></td>
</tr>
<tr>
<th>작성자</th>
<td><input type="text" name="writer" class="chk" title="작성자" value="${article.writer}"></td>
</tr>
<tr>
<th>글 내용</th>
<td><textarea name="content" class="chk" title="글내용">${article.content}</textarea></td>
</tr>
<tr>
<th>비밀번호</th>
<td><input type="password" name="passwd" class="chk" title="비밀번호" value="${article.passwd}"></td>
</tr>
<tr>
<td colspan="2">
<input class="btn" type="button" value="글 수정" id="submit11">
<input class="btn" type="reset" value="초기화">
<input class="btn" type="button" value="글 목록" onclick="location.href='list.do'">
</td>
</tr>
</table>
<input type="hidden" name="bno" value="${article.bno}">
<input type="hidden" name="bref" value="${article.bref}">
<input type="hidden" name="bstep" value="${article.bstep}">
<input type="hidden" name="blevel" value="${article.blevel}">
<input type="hidden" name="curPage" value="${pdto.curPage}">
</form>
</body>
</html>
글 수정 컨트롤러
package com.ecom4.hi.board.control;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ecom4.hi.HomeController;
import com.ecom4.hi.board.model.BoardDTO;
import com.ecom4.hi.board.model.PageDTO;
import com.ecom4.hi.board.model.RowInterPage;
import com.ecom4.hi.board.service.BoardService;
@Controller
public class BoardController {
private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
//JavaBoard에서 작업했던 Action 역할을 하는 객체 - 비즈니스 로직 BIZ를 갖고 있다
@Autowired
private BoardService boardService;
@RequestMapping(value = "/updateAction")
public String updateAction(HttpServletRequest request, HttpServletResponse response,
BoardDTO dto, Model model, PageDTO pdto) {
dto.setIp(request.getRemoteAddr());
int r = boardService.updateArticle(dto);
String msg ="";
if(r==0) {
msg = "수정 실패";
} else if(r==1) {
msg = "수정 성공";
}
model.addAttribute("article", dto);
model.addAttribute("pdto", pdto);
model.addAttribute("msg",msg);
model.addAttribute("page","list?curPage"+pdto.getCurPage());
return "board/MsgPage";
}
}
글수정 Service
BoardService 인터페이스
package com.ecom4.hi.board.service;
import java.util.List;
import java.util.Map;
import com.ecom4.hi.board.model.BoardDTO;
import com.ecom4.hi.board.model.PageDTO;
public interface BoardService {
int updateArticle(BoardDTO dto);
}
BoardServiceImpl 구현 클래스
package com.ecom4.hi.board.service;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ecom4.hi.board.dao.BoardDAO;
import com.ecom4.hi.board.model.BoardDTO;
import com.ecom4.hi.board.model.PageDTO;
import com.ecom4.hi.board.model.RowInterPage;
@Service(value = "boardService")
public class BoardServiceImpl implements BoardService {
// DAO DI(Dependency Injection)
@Autowired
private BoardDAO boardDao;
@Override
public int updateArticle(BoardDTO dto) {
return boardDao.updateArticle(dto);
}
}
글수정 DAO
package com.ecom4.hi.board.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ecom4.hi.board.model.BoardDTO;
@Repository(value = "boardDao")
public class BoardDAO {
// 자원을 DI (의존성 주입)
@Autowired
private SqlSession sqlSession;
private String namespace = "board.BoardDAO.";
public int updateArticle(BoardDTO dto) {
return sqlSession.update(namespace + "updateArticle",dto);
}
}
글 수정 Mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "board.BoardDAO">
<update id="updateArticle" parameterType="bvo">
UPDATE board
SET
subject = #{subject},
writer = #{writer},
content = #{content},
regdate = SYSDATE,
ip = #{ip},
passwd = #{passwd}
WHERE bno = #{bno}
</update>
</mapper>