<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
HandlerMapping mappings=new HandlerMapping();
controller=mappings.getController(command);
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(ViewResolver.makeView(view));
rd.forward(request, response);
}
}
}
}
<HandlerMapping.java>
package kr.board.frontcontroller;
import java.util.HashMap;
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;
public class HandlerMapping {
private HashMap<String, Controller> mappings;
public HandlerMapping() {
mappings=new HashMap<String, Controller>();
mappings.put("/boardList.do", new BoardListController());
mappings.put("/boardInsert.do", new BoardInsertController());
mappings.put("/boardForm.do", new BoardFormController());
mappings.put("/boardDelete.do", new BoardDeleteController());
mappings.put("/boardUpdateForm.do", new BoardUpdateFormController());
mappings.put("/boardUpdate.do", new BoardUpdateController());
mappings.put("/boardContent.do", new BoardContentController());
}
public Controller getController(String command) {
return mappings.get(command);
}
}
<ViewResolver.java>
package kr.board.frontcontroller;
public class ViewResolver {
// viewname -> WEB-INF/board/viewname.jsp
public static String makeView(String viewname) {
return "/WEB-INF/board/"+viewname+".jsp";
}
}
<boardList.jsp>
<%@page import="kr.board.domain.Board"%>
<%@page import="java.util.List"%>
<%@ 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 lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<jsp:include page = "header.jsp"/>
<h2>MVC Framework03</h2>
<div class="panel panel-default">
<div class="panel-heading">BOARD</div>
<div class="panel-body">
<table class="table table-bordered">
<tr>
<td>번호</td>
<td>제목</td>
<td>작성자</td>
<td>작성일</td>
<td>조회수</td>
</tr>
<c:forEach var="vo" items="${list}">
<tr>
<td>${vo.idx}</td>
<td><a href="boardContent.do?idx=${vo.idx}">${vo.title}</a></td>
<td>${vo.writer}</td>
<td>${vo.indate}</td>
<td>${vo.count}</td>
</tr>
</c:forEach>
</table>
<!--똑같음 : <button class="btn btn-success btn-sm">글쓰기</button> -->
<a href="boardForm.do" class="btn btn-success btn-sm">글쓰기</a>
</div>
<div class="panel-footer">지능형 IoT_최지연</div>
</div>
</div>
</body>
</html>
<Board.java>
package kr.board.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Board {
private int idx;
private String title;
private String content;
private String writer;
private String indate;
private int count;
}
<Controller.java - interface>
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;
}
<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);
dao.boardCount(idx); // 조회수 증가
request.setAttribute("vo", vo);
return "boardContent";
}
}
<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 "boardForm";
}
}
<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";
}
}
<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 "boardList";
}
}
<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 "boardUpdate"; //foward
}
}
'Maven Project' 카테고리의 다른 글
관리자용 게시판 관리 (0) | 2022.06.01 |
---|---|
HandlerMapping (0) | 2022.05.31 |
게시판 FrontController-command 방식 (0) | 2022.05.30 |
FrontController-command 방식 (0) | 2022.05.30 |
MVC Framework 게시판 (0) | 2022.05.27 |