SMALL
안녕하세요, 코린이의 코딩 학습기 채니 입니다.
개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다.
Dev 수정 구현하기
Controller
DemoController
@RequestMapping(path = "/updateDev.do", method = RequestMethod.GET)
public String updateDev(@RequestParam int no, Model model) {
Dev dev = demoService.selectOneDev(no);
model.addAttribute("dev", dev);
return "demo/devUpdateForm";
}
Service
DemoService interface 생략
DemoServiceImpl
@Override
public Dev selectOneDev(int no) {
return demoDao.selectOneDev(no);
}
Dao
DemoDao interface 생략
DemoDaoImpl
@Override
public Dev selectOneDev(int no) {
return sqlSession.selectOne("demo.selectOneDev", no);
}
demo-mapper.xml
<select id="selectOneDev" resultMap="devMap">
select * from dev where no = #{no}
</select>
<resultMap type="dev" id="devMap">
<result column="lang" property="lang"/> <!-- property가 String[] 타입이므로 설정해둔 핸들러가 자동으로 매칭되어 작동! -->
</resultMap>
devUpdateForm.jsp
<%@page import="java.util.List"%>
<%@page import="java.util.Arrays"%>
<%@page import="com.ce.spring2.demo.model.dto.Dev"%>
<%@ 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="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<jsp:include page="/WEB-INF/views/common/header.jsp">
<jsp:param value="Dev 수정" name="title"/>
</jsp:include>
<style>
div#demo-container{
width:550px;
padding:15px;
border:1px solid lightgray;
border-radius: 10px;
}
</style>
<div id="demo-container" class="mx-auto">
<!-- https://getbootstrap.com/docs/4.1/components/forms/#readonly-plain-text -->
<form name="devUpdateFrm"
method="post"
action="${pageContext.request.contextPath}/demo/updateDev.do">
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">이름</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" value="${dev.name}" required>
</div>
</div>
<div class="form-group row">
<label for="career" class="col-sm-2 col-form-label">개발경력</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="career" name="career" value="${dev.career}" required>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">이메일</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" value="${dev.email}" required>
</div>
</div>
<!-- https://getbootstrap.com/docs/4.1/components/forms/#inline -->
<div class="form-group row">
<label class="col-sm-2 col-form-label">성별</label>
<div class="col-sm-10">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender"
id="gender0" value="M" ${dev.gender eq 'M' ? 'checked' : ''}>
<label class="form-check-label" for="gender0">남</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender
id="gender1" value="F" ${dev.gender eq 'F' ? 'checked' : ''}>
<label class="form-check-label" for="gender1">여</label>
</div>
</div>
</div>
<%
String[] langs = ((Dev)request.getAttribute("dev")).getLang();
List<String> langList = Arrays.asList(langs);
pageContext.setAttribute("langList", langList);
%>
<div class="form-group row">
<label class="col-sm-2 col-form-label">개발언어</label>
<div class="col-sm-10">
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="lang" id="Java" value="Java" ${langList.contains('Java') ? 'checked' : ''} >
<label class="form-check-label" for="Java">Java</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="lang" id="C" value="C" ${langList.contains('C') ? 'checked' : ''} >
<label class="form-check-label" for="C">C</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="lang" id="Javascript" value="Javascript" ${langList.contains('Javascript') ? 'checked' : ''} >
<label class="form-check-label" for="Javascript">Javascript</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="lang" id="Python" value="Python" ${langList.contains('Python') ? 'checked' : ''} >
<label class="form-check-label" for="Python">Python</label>
</div>
</div>
</div>
<!-- 중요 - 수정시 반드시 고유번호도 함께 넘겨주어야 함 -->
<input type="hidden" name="no" value="${dev.no}" />
<button type="submit" class="list-group-item list-group-item-action">dev 수정</button>
</form>
</div>
<jsp:include page="/WEB-INF/views/common/footer.jsp"/>
정보 수정
Controller
DemoController
@RequestMapping(path = "/updateDev.do", method = RequestMethod.POST)
public String updateDev(Dev dev, RedirectAttributes redirectAttr) {
int result = demoService.updateDev(dev);
redirectAttr.addFlashAttribute("msg", "개발자 정보 수정!");
return "redirect:/demo/devList.do";
}
Service
DemoService interface 생략
DemoServiceImpl
@Override
public int updateDev(Dev dev) {
return demoDao.updateDev(dev);
}
Dao
DemoDao interface 생략
DemoDaoImpl
@Override
public int updateDev(Dev dev) {
return sqlSession.update("demo.updateDev", dev);
}
demo-mapper.xml
<update id="updateDev">
update dev set name = #{name}, career = #{career}, email = #{email}, gender = #{gender}, lang = #{lang} where no = #{no}
</update>
Dev 삭제 구현하기
Controller
DemoController
@RequestMapping(path = "/deleteDev.do", method = RequestMethod.POST)
public String deleteDev(@RequestParam int no, RedirectAttributes redirectAttr) {
int result = demoService.deleteDev(no);
redirectAttr.addFlashAttribute("msg", "개발자 정보 삭제!");
return "redirect:/demo/devList.do";
}
Service
DemoService interface 생략
DemoServiceImpl
@Override
public int deleteDev(int no) {
return demoDao.deleteDev(no);
}
Dao
DemoDao interface 생략
DemoDaoImpl
@Override
public int deleteDev(int no) {
return sqlSession.delete("demo.deleteDev", no);
}
demo-mapper.xml
<delete id="deleteDev">
delete from dev where no = #{no}
</delete>
LIST
'Java > └ [Spring] Practice' 카테고리의 다른 글
[실습문제] 게시글 수정 - 게시글 수정, 첨부파일 추가/삭제 (0) | 2022.09.05 |
---|