@WebServlet(urlPatterns = "/boards/*")
public class BoardServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (requestURI.equals("/boards/free")) { // 게시판 목록
viewListBoard(request, response);
} else if (requestURI.equals("/boards/free/boardId")) { // 게시글 상세보기
viewDetailBoard(request, response);
} else if (requestURI.equals("/board/free/write")) { // 게시글 생성
createBoard(request, response);
} else if (requestURI.equals("/board/free/update")) { // 게시글 수정
updateBoard(request, response);
} else if ...
{
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String requestURI = request.getRequestURI();
// 컨트롤러를 찾는 책임은 ControllerResolver 에게 위임
BoardController controller = controllerResolver.resolveController(requestURI);
if (controller == null) {
throw new RuntimeException(ErrorMessage.NO_HANDLER_MSG + request.getRequestURI());
}
// 단순히 컨트롤러를 실행하는 책임
controller.execute(request, response);
}
전체 코드: https://github.com/ji-jjang/ebrainsoft/tree/main/JspBoard
public interface BoardController {
public void execute(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException;
}
public class BoardCreateController implements BoardController {
private final CategoryDAO categoryDAO;
public BoardCreateController(CategoryDAO categoryDAO) {
this.categoryDAO = categoryDAO;
}
@Override
public void execute(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
List<String> categories = categoryDAO.getCategories();
req.setAttribute(Constants.CATEGORIES, categories);
req.getRequestDispatcher("/createBoard.jsp").forward(req, res);
}
}
public class BoardModifyController implements BoardController {
private final BoardDAO boardDAO = new BoardImpl();
private final BoardValidator validator = new BoardValidator();
private final BoardDAO boardDAO;
private final BoardValidator validator;
public BoardModifyController(BoardDAO boardDAO, BoardValidator validator) {
this.boardDAO = new boardDAOImpl();
this.validator = new Validtor();
}
public class BoardControllerFactory {
private final CategoryDAO categoryDAO;
private final BoardDAO boardDAO;
private final BoardValidator validator;
public BoardControllerFactory(
CategoryDAO categoryDAO, BoardDAO boardDAO, BoardValidator validator) {
this.categoryDAO = categoryDAO;
this.boardDAO = boardDAO;
this.validator = validator;
}
public Map<String, BoardController> createExactMappings() {
Map<String, BoardController> exactMappings = new HashMap<>();
exactMappings.put("/boards/free/write", new BoardCreateController(categoryDAO));
exactMappings.put(
"/boards/free/list", new BoardListController(boardDAO, categoryDAO, validator));
exactMappings.put("/boards/free/delete", new BoardDeleteController(validator));
return exactMappings;
}
public Map<Pattern, BoardController> createRegexMappings() {
Map<Pattern, BoardController> regexMappings = new HashMap<>();
regexMappings.put(
Pattern.compile("^/boards/free/view/[0-9]+"),
new BoardDetailController(boardDAO, validator));
regexMappings.put(
Pattern.compile("^/boards/free/modify/[0-9]+"),
new BoardModifyController(boardDAO, validator));
regexMappings.put(
Pattern.compile("^/boards/[0-9]+/comments$"),
new CommentCreateController(boardDAO, validator));
regexMappings.put(
Pattern.compile("^/boards/free/delete/[0-9]+"),
new BoardDeleteExecutionController(boardDAO, validator));
return regexMappings;
}
public BoardDAO createBoardDAO() {
return boardDAO;
}
public BoardValidator createBoardValidator() {
return validator;
}
}
전체 코드: https://github.com/ji-jjang/ebrainsoft/tree/main/JspBoard