<BoardFrontController.java>
package kr.board.frontcontroller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.board.controller.BoardContentController;
import kr.board.controller.BoardDeleteController;
import kr.board.controller.BoardFormController;
import kr.board.controller.BoardInsertController;
import kr.board.controller.BoardListController;
import kr.board.controller.BoardUpdateController;
import kr.board.controller.BoardUpdateFormController;
import kr.board.controller.Controller;
@WebServlet("*.do")
public class BoardFrontController extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. command구하기
String reqUrl = request.getRequestURI();
String ctx = request.getContextPath();
String command = reqUrl.substring(ctx.length());
// 2. command에 따른 분기 작업(if~else)
Controller controller = null;
String view = null;
// HandlerMapping
if (command.equals("/boardList.do")) {
controller = new BoardListController();
view = controller.execute(request, response);
} else if (command.equals("/boardInsert.do")) {
controller = new BoardInsertController();
view = controller.execute(request, response);
} else if (command.equals("/boardForm.do")) {
controller = new BoardFormController();
view = controller.execute(request, response);
} else if (command.equals("/boardDelete.do")) {
controller = new BoardDeleteController();
view = controller.execute(request, response);
} else if (command.equals("/boardUpdateForm.do")) {
controller = new BoardUpdateFormController();
view = controller.execute(request, response);
} else if (command.equals("/boardUpdate.do")) {
controller = new BoardUpdateController();
view = controller.execute(request, response);
} else if (command.equals("/boardContent.do")) {
controller = new BoardContentController();
view = controller.execute(request, response);
}
// 3. View의 경로를 보고 forward or redirect 결정
if (view != null) {
if (view.indexOf("redirect:/") != -1) {
response.sendRedirect(view.split(":/")[1]); // redirect:/boardList.do
} else {
RequestDispatcher rd = request.getRequestDispatcher(view);
rd.forward(request, response);
}
}
}
}
<BoardListController.java>
package kr.board.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.board.dao.BoardDAO;
import kr.board.domain.Board;
public class BoardListController implements Controller{
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Model과 연동하기
BoardDAO dao = new BoardDAO();
List<Board> list = dao.getAllList();
// 객체바인딩(*****)
request.setAttribute("list", list);
// boardList.jsp
return "/WEB-INF/board/boardList.jsp";
}
}
<BoardContentController.java>
package kr.board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.board.dao.BoardDAO;
import kr.board.domain.Board;
public class BoardContentController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ?idx=10
int idx = Integer.parseInt(request.getParameter("idx")); // "10" -> 10
BoardDAO dao = new BoardDAO();
Board vo = dao.boardContent(idx);
request.setAttribute("vo", vo);
return "/WEB-INF/board/boardContent.jsp";
}
}
<BoardDeleteController.java>
package kr.board.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.board.dao.BoardDAO;
public class BoardDeleteController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ?idx=10
int idx = Integer.parseInt(request.getParameter("idx"));
BoardDAO dao = new BoardDAO();
dao.boardDelete(idx);
return "redirect:/boardList.do"; //redirect
}
}
<BoardFormController.java>
package kr.board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BoardFormController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return "/WEB-INF/board/boardForm.jsp";
}
}
<BoardInsertController.java>
package kr.board.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.board.dao.BoardDAO;
import kr.board.domain.Board;
public class BoardInsertController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 파라메터수집(VO)
String title = request.getParameter("title");
String content = request.getParameter("content");
String writer = request.getParameter("writer");
Board vo = new Board();
vo.setTitle(title);
vo.setContent(content);
vo.setWriter(writer);
BoardDAO dao = new BoardDAO();
dao.boardInsert(vo); //등록
// 등록후에는 다시 리스트페이지로 ... boardList.do
return "redirect:/boardList.do";
}
}
<BoardUpdateController.java>
package kr.board.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.board.dao.BoardDAO;
import kr.board.domain.Board;
public class BoardUpdateController implements Controller{
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String content = request.getParameter("content");
int idx = Integer.parseInt(request.getParameter("idx"));
Board vo = new Board();
vo.setTitle(title);
vo.setContent(content);
vo.setIdx(idx);
BoardDAO dao = new BoardDAO();
dao.boardUpdate(vo); //등록
return "redirect:/boardList.do";
}
}
<BoardUpdateFormController.java>
package kr.board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.board.dao.BoardDAO;
import kr.board.domain.Board;
public class BoardUpdateFormController implements Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int idx = Integer.parseInt(request.getParameter("idx"));
BoardDAO dao = new BoardDAO();
dao.boardContent(idx);
Board vo = dao.boardContent(idx);
request.setAttribute("vo", vo);
return "/WEB-INF/board/boardUpdate.jsp"; //foward
}
}
<Controller.java>
package kr.board.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
'Maven Project' 카테고리의 다른 글
관리자용 게시판 관리 (0) | 2022.06.01 |
---|---|
HandlerMapping (0) | 2022.05.31 |
FrontController-command 방식 (0) | 2022.05.30 |
MVC Framework 게시판 (0) | 2022.05.27 |
게시판 글 삭제 (0) | 2022.05.27 |