-
[김영한 스프링] 14. MVC 프레임워크 만들기 - View 분리Spring/스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술 2023. 6. 29. 03:35
View 분리 - v2
V2 구조
MyView
main/java/hello.servlet/web/frontcontroller/MyView 생성
package hello.servlet.web.frontcontroller; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class MyView { private String viewPath; public MyView(String viewPath) { this.viewPath = viewPath; } public void render(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath); dispatcher.forward(request, response); } }
ControllerV2
package hello.servlet.web.frontcontroller.v2; import hello.servlet.web.frontcontroller.MyView; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public interface ControllerV2 { MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; }
main/java/hello.servlet/web/frontcontroller/v2/ControllerV2 생성
MemberFormControllerV2 - 회원 등록 폼
main/java/hello.servlet/web/frontcontroller/v2/controller/MemberFormControllerV2 생성
package hello.servlet.web.frontcontroller.v2.controller; import hello.servlet.web.frontcontroller.MyView; import hello.servlet.web.frontcontroller.v2.ControllerV2; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class MemberFormControllerV2 implements ControllerV2 { @Override public MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String viewPath = "/WEB-INF/views/new-form.jsp"; // RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath); // dispatcher.forward(request, response); return new MyView("/WEB-INF/views/new-form.jsp"); } }
이제 각 컨트롤러는 복잡한 dispatcher.forward()를 직접 생성해서 호출하지 않아도 된다. 단순히 MyView 객체를 생성하고 거기에 뷰 이름만 넣고 반환하면 된다.
MemberSaveControllerV2 - 회원 저장
main/java/hello.servlet/web/frontcontroller/v2/controller/MemberSaveControllerV2 생성
package hello.servlet.web.frontcontroller.v2.controller; import hello.servlet.domain.member.Member; import hello.servlet.domain.member.MemberRepository; import hello.servlet.web.frontcontroller.MyView; import hello.servlet.web.frontcontroller.v2.ControllerV2; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class MemberSaveControllerV2 implements ControllerV2 { MemberRepository memberRepository = MemberRepository.getInstance(); @Override public MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); int age = Integer.parseInt(request.getParameter("age")); Member member = new Member(username, age); memberRepository.save(member); // Model에 데이터를 보관한다. request.setAttribute("member", member); // String viewPath = "/WEB-INF/views/save-result.jsp"; // RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath); // dispatcher.forward(request, response); return new MyView("/WEB-INF/views/save-result.jsp"); } }
MemberListControllerV2 - 회원 목록
main/java/hello.servlet/web/frontcontroller/v2/controller/MemberListControllerV2 생성
package hello.servlet.web.frontcontroller.v2.controller; import hello.servlet.domain.member.Member; import hello.servlet.domain.member.MemberRepository; import hello.servlet.web.frontcontroller.MyView; import hello.servlet.web.frontcontroller.v2.ControllerV2; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; public class MemberListControllerV2 implements ControllerV2 { MemberRepository memberRepository = MemberRepository.getInstance(); @Override public MyView process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Member> members = memberRepository.findAll(); request.setAttribute("members", members); // String viewPath = "/WEB-INF/views/members.jsp"; // RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath); // dispatcher.forward(request, response); return new MyView("/WEB-INF/views/members.jsp"); } }
프론트 컨트롤러 V2
main/java/hello.servlet/web/frontcontroller/v2/FrontControllerServletV2 생성
package hello.servlet.web.frontcontroller.v2; import hello.servlet.web.frontcontroller.MyView; import hello.servlet.web.frontcontroller.v2.controller.MemberFormControllerV2; import hello.servlet.web.frontcontroller.v2.controller.MemberListControllerV2; import hello.servlet.web.frontcontroller.v2.controller.MemberSaveControllerV2; 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 java.io.IOException; import java.util.HashMap; import java.util.Map; @WebServlet(name = "frontControllerServletV2", urlPatterns = "/front-controller/v2/*") public class FrontControllerServletV2 extends HttpServlet { private Map<String, ControllerV2> controllerMap = new HashMap<>(); public FrontControllerServletV2() { controllerMap.put("/front-controller/v2/members/new-form", new MemberFormControllerV2()); controllerMap.put("/front-controller/v2/members/save", new MemberSaveControllerV2()); controllerMap.put("/front-controller/v2/members", new MemberListControllerV2()); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("FrontControllerServletV2.service"); String requestURI = request.getRequestURI(); ControllerV2 controller = controllerMap.get(requestURI); if (controller == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } MyView view = controller.process(request, response); view.render(request, response); } }
ControllerV2의 반환 타입이 MyView 이므로 프론트 컨트롤러는 컨트롤러의 호출 결과로 MyView를 반환받는다. 그리고 view.render()를 호출하면 forward 로직을 수행해서 JSP가 실행된다.
실행
출처 : https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1
'Spring > 스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술' 카테고리의 다른 글
[김영한 스프링] 16. MVC 프레임워크 만들기 - 단순하고 실용적인 컨트롤러 (0) 2023.06.30 [김영한 스프링] 15. MVC 프레임워크 만들기 - Model 분리 (0) 2023.06.30 [김영한 스프링] 13. MVC 프레임워크 만들기 - 프론트 컨트롤러 소개&도입 (0) 2023.06.29 [김영한 스프링] 12. MVC 패턴 (0) 2023.06.28 [김영한 스프링] 11. JSP로 회원 관리 웹 애플리케이션 만들기 (0) 2023.06.22