55일차(3)/Spring(17) : 게시판 글 삭제, 수정 기능 구현
- delete 메소드 추가
- update 메소드 추가 / updateform, update 페이지 추가
controller
@RequestMapping("/cafe/delete")
public String delete(int num, HttpServletRequest request) {
service.deleteContent(num, request);
return "redirect:/cafe/list";
}
- list로 리다이렉트 시켜버리기
Service
@Override
public void deleteContent(int num, HttpServletRequest request) {
cafeDao.delete(num);
}
- 메소드의 인자로 받아온 request는 다른사람의 글을 지우지 못하도록 막는 역할을 한다!
- 나중에 한번에 예외처리할 때 수정할 것
- 수정 기능 추가
controller
- 글 하나의 정보를 request 영역에 담아서 가져온다.
Service
@Override
public void getData(HttpServletRequest request) {
//수정할 글번호
int num=Integer.parseInt(request.getParameter("num"));
//수정할 글의 정보 얻어와서
CafeDto dto=cafeDao.getData(num);
//request에 담아준다.
request.setAttribute("dto", dto);
}
- 리퀘스트 영역에 담은 패러미터 값을 num에 담아주고,
getData에 넣어 수정할 글 정보를 가져온다.
- request영역에 dto 값을 암자준다.
updateform.jsp
- textarea의 innertext에 content 를 출력해주어야 한다.
- 폼 페이지에 들어가보면 수정할 값이 제대로 들어와있는 것을 볼 수 있다.
컨트롤러
update 메소드
@RequestMapping("/cafe/update")
public String update(CafeDto dto) {
service.updateContent(dto);
return "cafe/update";
}
- dto를 사용해서 서비스 메소드를 실행해준다.
서비스
@Override
public void updateContent(CafeDto dto) {
cafeDao.update(dto);
}
- 단순 수정반영만 하면 된다.
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>/views/cafe/update.jsp</title>
</head>
<body>
<script>
alert("${id}님 글을 수정했습니다.");
location.href="${pageContext.request.contextPath}/cafe/detail?num=${param.num}";
</script>
</body>
</html>
Q) update.jsp 뷰페이지로 이동했을 때 가져가는 값(dto)는 num, title, content 값인데
값을 ${id} 로 가져올 수 있는 이유는?
그리고 ${param.num} 도 사용이 가능한 이유는?
- interceptor에서 확인해보면, cafe 하위 요청은 로그인해야만 들어올 수 있다.
이미 session 영역에 로그인정보(id)가 저장되어 있는 상태라고 보면 된다.
- 폼이 제출되면 기본으로 num, title, content가 넘어온다
- 이걸 dto에 담아서 view page로 이동하는데, 필요하다면 이 값들을 사용할 수 있다.
parameter로 전달되었기 때문에! ${param.xx} 로 사용 가능하다.
- 포워드 이동된 view 페이지에서도 필요하다면 request.getParameter로 파라미터 값을 추출해서 사용할 수 있다.
'국비교육(22-23)' 카테고리의 다른 글
56일차(1)/Spring Boot(2) : Bean 생성하는 방법, AOP 활용 실습 (0) | 2022.12.26 |
---|---|
55일차(4)/Spring Boot(1) : Spring Boot 기초, AOP의 구조 익히기 (0) | 2022.12.26 |
55일차(2)/Spring(16) : 글 상세보기, 이전글/다음글 불러오기 (0) | 2022.12.25 |
55일차(1)/Spring(15) : Cafe 게시판 기능 구현 (목록보기, 새글 작성) (1) | 2022.12.25 |
54일차(2)/Spring(14) : 자료실 파일 다운로드, 삭제, 검색 기능 구현 (0) | 2022.12.22 |