일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- error
- 깃 토큰
- csv
- 따옴표 삭제
- cmd
- php
- DataGrip
- github clone
- 오류
- PHPStorm
- error 해결
- 파이썬
- Visual Studio Code
- MySQL
- 데이터베이스
- run sql script
- clone
- vscode
- 단축키
- github token
- localhost
- Python
- OrCAD 다운로드
- database
- visualstudio code
- 클론
- import data
- jupyter
- 에러
- console창
- Today
- Total
개발 노트
9/27 : MVC 모델2 제작(4/4) 본문
9/27 : MVC 모델2 제작(4/4)
hayoung.dev 2022. 10. 20. 21:56command.properties에 /deleteForm.do=service.DeleteFormAction 추가
src/main/java > service 패키지 > DeleteFormAction.java(class), CommandProcess 상속받음
package service;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.Board;
import dao.BoardDao;
public class DeleteFormAction implements CommandProcess {
@Override
public String requestPro(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("DeleteFormAction start...");
try {
// 1. num , pageNum Get
int num = Integer.parseInt(request.getParameter("num"));
String pageNum = request.getParameter("pageNum");
// 2. BoardDao bd Instance
BoardDao bd = BoardDao.getInstance();
// 3. Board board = bd.select(num);
Board board = bd.select(num);
// 4. request 객체에 num , pageNum ,board
request.setAttribute("num", num);
request.setAttribute("pageNum", pageNum);
request.setAttribute("board", board);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return "deleteForm.jsp";
}
}
webapp > deleteForm.jsp(jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>삭제하려면 암호를 입력하세요</h2>
<form action="deletePro.do">
<input type="hidden" name="pageNum" value="${pageNum }">
<input type="hidden" name="num" value="${num }">
암호 : <input type="text" name="passwd"><p>
<input type="submit" value="확인">
</form>
</body>
</html>
command.properties에 /deletePro.do=service.DeleteProAction 추가
src/main/java > service 패키지 > DeleteProAction.java(class)
package service;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BoardDao;
public class DeleteProAction implements CommandProcess {
@Override
public String requestPro(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("DeleteProAction Start...");
try {
// 1. num , passwd , pageNum Get
int num = Integer.parseInt(request.getParameter("num"));
String passwd = request.getParameter("passwd");
String pageNum = request.getParameter("pageNum");
// 2. BoardDao bd Instance
BoardDao bd = BoardDao.getInstance();
// 3. 본인의 게시판 만 삭제
int result = bd.delete(num, passwd);
// 4. request 객체에 num , pageNum ,result
request.setAttribute("result", result);
request.setAttribute("pageNum", pageNum);
request.setAttribute("num", num);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return "deletePro.jsp";
}
}
webapp > deletePro.jsp(jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${result > 0 }">
<script type="text/javascript">
alert("삭제 완료 ! ");
location.href="list.do?pageNum=${pageNum}";
</script>
</c:if>
<c:if test="${result == 0 }">
<script type="text/javascript">
alert("헐 ~ ㅠㅠ 암호 틀려");
location.href="deleteForm.do?num=${num}&pageNum=${pageNum}";
</script>
</c:if>
</body>
</html>
BoardDao.java에 delete(int num, String passwd) 코드추가
실행 결과 : 글 삭제됨
command.properties에 /ajaxGetDeptName.do=service.AjaxGetDeptNameAction 추가
src/main/java > service 패키지 > AjaxGetDeptNameAction.java(class)
package service;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.Board;
import dao.BoardDao;
public class AjaxGetDeptNameAction implements CommandProcess {
@Override
public String requestPro(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("AjaxGetDeptNameAction Start...");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
try {
// 본인 필요 DB Text 가져옴 (DAO 연결)
int num = Integer.parseInt(request.getParameter("num"));
BoardDao bd = BoardDao.getInstance();
Board board = bd.select(num);
request.setAttribute("writer", board.getWriter());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
//ajax 경우 --> 더미 Return
return "ajax";
}
}
[최종 mvc 모델2 순서 정리]
1. view에서 .do 호출
-> web.xml 설정으로 컨트롤러 실행됨(컨트롤러는 코드 수정 없이 그대로 사용)
-> controller 설정으로 command.properties(file로 제작) 키에 해당하는 값인 service 확인
2. 해당 service 실행(class로 제작)
-> 여기에서 DTO, DAO 실행
3. 실행 다하면 return으로 view로 이동
즉 view에서 service로 가려면 .do를 씀. service에서는 return을 통해 view로 이동.
'프로젝트 기반 JAVA 응용 SW개발 : 22.07.19~23.01.20 > Servlet, JSP' 카테고리의 다른 글
9/26 : MVC 모델2 제작(3/4) (0) | 2022.10.04 |
---|---|
9/23 : MVC 모델2 제작(2/4) (0) | 2022.10.04 |
9/22 : MVC 모델1 제작(3/3), MVC 모델2 제작(1/4) (0) | 2022.10.04 |
9/21 : MVC 모델1 제작(2/3) (0) | 2022.10.04 |
9/20 : MVC 모델1 제작(1/3) (0) | 2022.10.04 |