From 4d286071d3bd8fdd13660f1c4c715ab3d97b4e4f Mon Sep 17 00:00:00 2001 From: wugawuga Date: Sun, 24 Sep 2023 16:15:51 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20jspView=20render=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 22 +++++-------------- .../web/servlet/view/JspView.java | 10 ++++++--- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 914441b202..00c9da3e78 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -4,11 +4,9 @@ import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { @@ -37,26 +35,18 @@ protected void service(final HttpServletRequest request, final HttpServletRespon try { final var handler = handlerMappings.getHandler(request); final var modelAndView = handlerAdaptors.execute(handler, request, response); - move(modelAndView, request, response); + render(modelAndView, request, response); } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); throw new ServletException(e.getMessage()); } } - private void move(final ModelAndView modelAndView, final HttpServletRequest request, - final HttpServletResponse response) - throws Exception { - if (Objects.isNull(modelAndView.getViewName())) { - // TODO JsonView - return; - } - if (modelAndView.getViewName().startsWith(JspView.REDIRECT_PREFIX)) { - response.sendRedirect(modelAndView.getViewName().substring(JspView.REDIRECT_PREFIX.length())); - return; - } + private void render(final ModelAndView modelAndView, final HttpServletRequest request, + final HttpServletResponse response) throws Exception { + final var view = modelAndView.getView(); + final var model = modelAndView.getModel(); - final var requestDispatcher = request.getRequestDispatcher(modelAndView.getViewName()); - requestDispatcher.forward(request, response); + view.render(model, request, response); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java index 230e6aeac5..f021a2819e 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java @@ -22,14 +22,18 @@ public JspView(final String viewName) { @Override public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - // todo - model.keySet().forEach(key -> { log.debug("attribute name : {}, value : {}", key, model.get(key)); request.setAttribute(key, model.get(key)); }); - // todo + if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { + response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); + return; + } + + final var requestDispatcher = request.getRequestDispatcher(viewName); + requestDispatcher.forward(request, response); } public String getViewName() { From d01ef67d3d429fbc1957b33b35e3b3c3bb2d3582 Mon Sep 17 00:00:00 2001 From: wugawuga Date: Sun, 24 Sep 2023 17:16:01 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20legacy=20mvc=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/ManualHandlerMapping.java | 6 ---- .../controller/LoginController.java | 30 ++++++++++++++++--- .../controller/LoginViewController.java | 22 -------------- .../controller/LogoutController.java | 15 ---------- .../web/servlet/ModelAndView.java | 4 --- .../org/springframework/web/servlet/View.java | 2 -- .../web/servlet/view/JsonView.java | 5 ---- .../web/servlet/view/JspView.java | 14 ++++----- 8 files changed, 31 insertions(+), 67 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/controller/LoginViewController.java delete mode 100644 app/src/main/java/com/techcourse/controller/LogoutController.java diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java index 063b08070d..4738119e92 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/ManualHandlerMapping.java @@ -1,8 +1,5 @@ package com.techcourse; -import com.techcourse.controller.LoginController; -import com.techcourse.controller.LoginViewController; -import com.techcourse.controller.LogoutController; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; @@ -18,9 +15,6 @@ public class ManualHandlerMapping { public void initialize() { controllers.put("/", new ForwardController("/index.jsp")); - controllers.put("/login", new LoginController()); - controllers.put("/login/view", new LoginViewController()); - controllers.put("/logout", new LogoutController()); controllers.put("/404", new ForwardController("/404.jsp")); log.info("Initialized Handler Mapping!"); diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 0428fe109e..e903a14c6b 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,18 +2,23 @@ import com.techcourse.domain.User; import com.techcourse.repository.InMemoryUserRepository; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; -public class LoginController implements Controller { +@Controller +public class LoginController { private static final Logger log = LoggerFactory.getLogger(LoginController.class); - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(value = "/login", method = RequestMethod.POST) + public String login(final HttpServletRequest req, final HttpServletResponse res) throws Exception { if (UserSession.isLoggedIn(req.getSession())) { return "redirect:/index.jsp"; } @@ -26,6 +31,23 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re .orElse("redirect:/401.jsp"); } + @RequestMapping(value = "/login/view", method = RequestMethod.GET) + public ModelAndView loginView(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + return UserSession.getUserFrom(req.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return new ModelAndView(new JspView("redirect:/index.jsp")); + }) + .orElse(new ModelAndView(new JspView("/login.jsp"))); + } + + @RequestMapping(value = "/logout", method = RequestMethod.POST) + public String logout(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + final var session = req.getSession(); + session.removeAttribute(UserSession.SESSION_KEY); + return "redirect:/"; + } + private String login(final HttpServletRequest request, final User user) { if (user.checkPassword(request.getParameter("password"))) { final var session = request.getSession(); diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java deleted file mode 100644 index 86ec26cdce..0000000000 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.techcourse.controller; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; - -public class LoginViewController implements Controller { - - private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - return UserSession.getUserFrom(req.getSession()) - .map(user -> { - log.info("logged in {}", user.getAccount()); - return "redirect:/index.jsp"; - }) - .orElse("/login.jsp"); - } -} diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java deleted file mode 100644 index 4642fd9450..0000000000 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.techcourse.controller; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; - -public class LogoutController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - final var session = req.getSession(); - session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; - } -} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java index d5340b02f9..ff8e24553f 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java @@ -30,8 +30,4 @@ public Map getModel() { public View getView() { return view; } - - public String getViewName() { - return this.view.getViewName(); - } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java index c9679e103d..e3dcec2408 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java @@ -6,6 +6,4 @@ public interface View { void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; - - String getViewName(); } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java index 76c495a29c..522af7f911 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java @@ -11,9 +11,4 @@ public class JsonView implements View { public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { } - - @Override - public String getViewName() { - return null; - } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java index f021a2819e..0b34077a3e 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java @@ -22,21 +22,17 @@ public JspView(final String viewName) { @Override public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - model.keySet().forEach(key -> { - log.debug("attribute name : {}, value : {}", key, model.get(key)); - request.setAttribute(key, model.get(key)); - }); - if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); return; } + model.keySet().forEach(key -> { + log.debug("attribute name : {}, value : {}", key, model.get(key)); + request.setAttribute(key, model.get(key)); + }); + final var requestDispatcher = request.getRequestDispatcher(viewName); requestDispatcher.forward(request, response); } - - public String getViewName() { - return viewName; - } } From b1cc12c455def916604c2c7b6c0707657a92376b Mon Sep 17 00:00:00 2001 From: wugawuga Date: Sun, 24 Sep 2023 20:36:47 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20legacy=20mvc=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 - .../main/java/com/techcourse/Application.java | 14 +++--- .../DispatcherServletInitializer.java | 7 +-- .../java/com/techcourse/HandlerAdaptor.java | 13 ------ .../java/com/techcourse/HandlerAdaptors.java | 26 ----------- .../com/techcourse/LegacyHandlerAdaptor.java | 24 ----------- .../com/techcourse/ManualHandlerMapping.java | 29 ------------- .../controller/LoginController.java | 10 ++--- .../techcourse/controller/PageController.java | 23 ++++++++++ .../controller/RegisterController.java | 8 ++-- mvc/build.gradle | 2 + .../springframework/web}/TomcatStarter.java | 12 ++---- .../web/servlet}/DispatcherServlet.java | 12 ++++-- .../mvc}/UncheckedServletException.java | 2 +- .../web/servlet/mvc/asis/Controller.java | 8 ---- .../servlet/mvc/asis/ForwardController.java | 20 --------- .../AnnotationHandlerMapping.java | 12 +++++- .../mvc/handermapping}/HandlerMappings.java | 17 ++------ .../ExecutionHandleradaptor.java | 10 ++--- .../mvc/handlerAdaptor/HandlerAdaptors.java | 43 +++++++++++++++++++ .../HandlerExecution.java | 7 ++- .../{tobe => handlerAdaptor}/HandlerKey.java | 13 +++--- .../mvc/handlerAdaptor/Handleradaptor.java | 12 ++++++ .../web/servlet/mvc/view/JsonView.java | 28 ++++++++++++ .../web/servlet/{ => mvc}/view/JspView.java | 3 +- .../servlet/{ => mvc/view}/ModelAndView.java | 2 +- .../web/servlet/{ => mvc/view}/View.java | 2 +- .../web/servlet/view/JsonView.java | 14 ------ mvc/src/test/java/samples/TestController.java | 4 +- .../tobe/AnnotationHandlerMappingTest.java | 7 ++- 30 files changed, 179 insertions(+), 206 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/HandlerAdaptor.java delete mode 100644 app/src/main/java/com/techcourse/HandlerAdaptors.java delete mode 100644 app/src/main/java/com/techcourse/LegacyHandlerAdaptor.java delete mode 100644 app/src/main/java/com/techcourse/ManualHandlerMapping.java create mode 100644 app/src/main/java/com/techcourse/controller/PageController.java rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web}/TomcatStarter.java (93%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet}/DispatcherServlet.java (77%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/UncheckedServletException.java (73%) delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java rename mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/{tobe => handermapping}/AnnotationHandlerMapping.java (86%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handermapping}/HandlerMappings.java (50%) rename app/src/main/java/com/techcourse/ExecutionHandlerAdaptor.java => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/ExecutionHandleradaptor.java (52%) create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerAdaptors.java rename mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/{tobe => handlerAdaptor}/HandlerExecution.java (66%) rename mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/{tobe => handlerAdaptor}/HandlerKey.java (80%) create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/Handleradaptor.java create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/JsonView.java rename mvc/src/main/java/webmvc/org/springframework/web/servlet/{ => mvc}/view/JspView.java (91%) rename mvc/src/main/java/webmvc/org/springframework/web/servlet/{ => mvc/view}/ModelAndView.java (92%) rename mvc/src/main/java/webmvc/org/springframework/web/servlet/{ => mvc/view}/View.java (82%) delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java diff --git a/app/build.gradle b/app/build.gradle index b96dc81587..1fe7950384 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,7 +17,6 @@ repositories { dependencies { implementation project(":mvc") - implementation "org.apache.tomcat.embed:tomcat-embed-core:10.1.13" implementation "org.apache.tomcat.embed:tomcat-embed-jasper:10.1.13" implementation "ch.qos.logback:logback-classic:1.2.12" implementation "org.apache.commons:commons-lang3:3.13.0" diff --git a/app/src/main/java/com/techcourse/Application.java b/app/src/main/java/com/techcourse/Application.java index 500d3f2d8b..5a4c37dc64 100644 --- a/app/src/main/java/com/techcourse/Application.java +++ b/app/src/main/java/com/techcourse/Application.java @@ -1,10 +1,10 @@ package com.techcourse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; import java.util.stream.Stream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import webmvc.org.springframework.web.TomcatStarter; public class Application { @@ -12,10 +12,12 @@ public class Application { private static final int DEFAULT_PORT = 8080; - public static void main(final String[] args) throws Exception { + private static final String WEBAPP_DIR_LOCATION = "app/src/main/webapp/"; + + public static void main(final String[] args) { final int port = defaultPortIfNull(args); - final var tomcat = new TomcatStarter(port); - log.info("configuring app with basedir: {}", TomcatStarter.WEBAPP_DIR_LOCATION); + final var tomcat = new TomcatStarter(WEBAPP_DIR_LOCATION, port); + log.info("configuring app with basedir: {}", WEBAPP_DIR_LOCATION); tomcat.start(); stop(tomcat); diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 6e814cdd25..d4574a871b 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -4,10 +4,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.web.WebApplicationInitializer; +import webmvc.org.springframework.web.servlet.DispatcherServlet; /** - * Base class for {@link WebApplicationInitializer} - * implementations that register a {@link DispatcherServlet} in the servlet context. + * Base class for {@link WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in the + * servlet context. */ public class DispatcherServletInitializer implements WebApplicationInitializer { @@ -17,7 +18,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer { @Override public void onStartup(final ServletContext servletContext) { - final var dispatcherServlet = new DispatcherServlet(); + final var dispatcherServlet = new DispatcherServlet(this.getClass().getPackageName()); final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet); if (registration == null) { diff --git a/app/src/main/java/com/techcourse/HandlerAdaptor.java b/app/src/main/java/com/techcourse/HandlerAdaptor.java deleted file mode 100644 index 05b2ade2de..0000000000 --- a/app/src/main/java/com/techcourse/HandlerAdaptor.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.techcourse; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; - -public interface HandlerAdaptor { - - boolean isSame(final Object handler); - - ModelAndView execute(final Object handler, final HttpServletRequest request, final HttpServletResponse response) - throws Exception; -} diff --git a/app/src/main/java/com/techcourse/HandlerAdaptors.java b/app/src/main/java/com/techcourse/HandlerAdaptors.java deleted file mode 100644 index ba677cd250..0000000000 --- a/app/src/main/java/com/techcourse/HandlerAdaptors.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.techcourse; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.util.List; -import webmvc.org.springframework.web.servlet.ModelAndView; - -public class HandlerAdaptors { - - private final List handlerAdaptors; - - public HandlerAdaptors() { - this.handlerAdaptors = List.of(new LegacyHandlerAdaptor(), new ExecutionHandlerAdaptor()); - } - - public ModelAndView execute(final Object handler, final HttpServletRequest request, - final HttpServletResponse response) - throws Exception { - final HandlerAdaptor findHandlerAdaptor = handlerAdaptors.stream() - .filter(adaptor -> adaptor.isSame(handler)) - .findFirst() - .orElseThrow(IllegalArgumentException::new); - - return findHandlerAdaptor.execute(handler, request, response); - } -} diff --git a/app/src/main/java/com/techcourse/LegacyHandlerAdaptor.java b/app/src/main/java/com/techcourse/LegacyHandlerAdaptor.java deleted file mode 100644 index 3fa3434532..0000000000 --- a/app/src/main/java/com/techcourse/LegacyHandlerAdaptor.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.techcourse; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.view.JspView; - -public class LegacyHandlerAdaptor implements HandlerAdaptor { - - @Override - public boolean isSame(final Object handler) { - return handler instanceof Controller; - } - - @Override - public ModelAndView execute(final Object handler, final HttpServletRequest request, - final HttpServletResponse response) - throws Exception { - final Controller controller = (Controller) handler; - final String viewName = controller.execute(request, response); - return new ModelAndView(new JspView(viewName)); - } -} diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java deleted file mode 100644 index 4738119e92..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.techcourse; - -import java.util.HashMap; -import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; - -public class ManualHandlerMapping { - - private static final Logger log = LoggerFactory.getLogger(ManualHandlerMapping.class); - - private static final Map controllers = new HashMap<>(); - - public void initialize() { - controllers.put("/", new ForwardController("/index.jsp")); - controllers.put("/404", new ForwardController("/404.jsp")); - - log.info("Initialized Handler Mapping!"); - controllers.keySet() - .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); - } - - public Controller getHandler(final String requestURI) { - log.debug("Request Mapping Uri : {}", requestURI); - return controllers.get(requestURI); - } -} diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index e903a14c6b..292a554025 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -9,8 +9,8 @@ import org.slf4j.LoggerFactory; import web.org.springframework.web.bind.annotation.RequestMapping; import web.org.springframework.web.bind.annotation.RequestMethod; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; +import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.view.JspView; @Controller public class LoginController { @@ -18,7 +18,7 @@ public class LoginController { private static final Logger log = LoggerFactory.getLogger(LoginController.class); @RequestMapping(value = "/login", method = RequestMethod.POST) - public String login(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public String login(final HttpServletRequest req, final HttpServletResponse res) { if (UserSession.isLoggedIn(req.getSession())) { return "redirect:/index.jsp"; } @@ -32,7 +32,7 @@ public String login(final HttpServletRequest req, final HttpServletResponse res) } @RequestMapping(value = "/login/view", method = RequestMethod.GET) - public ModelAndView loginView(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public ModelAndView loginView(final HttpServletRequest req, final HttpServletResponse res) { return UserSession.getUserFrom(req.getSession()) .map(user -> { log.info("logged in {}", user.getAccount()); @@ -42,7 +42,7 @@ public ModelAndView loginView(final HttpServletRequest req, final HttpServletRes } @RequestMapping(value = "/logout", method = RequestMethod.POST) - public String logout(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public String logout(final HttpServletRequest req, final HttpServletResponse res) { final var session = req.getSession(); session.removeAttribute(UserSession.SESSION_KEY); return "redirect:/"; diff --git a/app/src/main/java/com/techcourse/controller/PageController.java b/app/src/main/java/com/techcourse/controller/PageController.java new file mode 100644 index 0000000000..bd11178baf --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/PageController.java @@ -0,0 +1,23 @@ +package com.techcourse.controller; + +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.view.JspView; + +@Controller +public class PageController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView index(final HttpServletRequest req, final HttpServletResponse res) { + return new ModelAndView(new JspView("/index.jsp")); + } + + @RequestMapping(value = "/404", method = RequestMethod.GET) + public ModelAndView notFoundError(final HttpServletRequest req, final HttpServletResponse res) { + return new ModelAndView(new JspView("/404.jsp")); + } +} diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 8d9a9d6358..f5814ad15c 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -7,19 +7,19 @@ import jakarta.servlet.http.HttpServletResponse; import web.org.springframework.web.bind.annotation.RequestMapping; import web.org.springframework.web.bind.annotation.RequestMethod; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; +import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.view.JspView; @Controller public class RegisterController { @RequestMapping(value = "/register/view", method = RequestMethod.GET) - public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) { return new ModelAndView(new JspView("/register.jsp")); } @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView save(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public ModelAndView save(final HttpServletRequest req, final HttpServletResponse res) { final var user = new User(2, req.getParameter("account"), req.getParameter("password"), diff --git a/mvc/build.gradle b/mvc/build.gradle index 8cdd011952..98481ed168 100644 --- a/mvc/build.gradle +++ b/mvc/build.gradle @@ -11,6 +11,8 @@ repositories { } dependencies { + implementation "org.apache.tomcat.embed:tomcat-embed-core:10.1.13" + implementation "jakarta.servlet:jakarta.servlet-api:5.0.0" implementation "javax.servlet.jsp:javax.servlet.jsp-api:2.3.3" implementation "jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api:3.0.0" diff --git a/app/src/main/java/com/techcourse/TomcatStarter.java b/mvc/src/main/java/webmvc/org/springframework/web/TomcatStarter.java similarity index 93% rename from app/src/main/java/com/techcourse/TomcatStarter.java rename to mvc/src/main/java/webmvc/org/springframework/web/TomcatStarter.java index 4f26f228e3..cffd98de11 100644 --- a/app/src/main/java/com/techcourse/TomcatStarter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/TomcatStarter.java @@ -1,24 +1,18 @@ -package com.techcourse; +package webmvc.org.springframework.web; +import java.io.File; import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; import org.apache.catalina.connector.Connector; import org.apache.catalina.core.StandardContext; import org.apache.catalina.startup.Tomcat; import org.apache.tomcat.util.scan.StandardJarScanner; - -import java.io.File; +import webmvc.org.springframework.web.servlet.mvc.UncheckedServletException; public class TomcatStarter { - public static final String WEBAPP_DIR_LOCATION = "app/src/main/webapp/"; - private final Tomcat tomcat; - public TomcatStarter(final int port) { - this(WEBAPP_DIR_LOCATION, port); - } - public TomcatStarter(final String webappDirLocation, final int port) { this.tomcat = new Tomcat(); tomcat.setConnector(createConnector(port)); diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/DispatcherServlet.java similarity index 77% rename from app/src/main/java/com/techcourse/DispatcherServlet.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/DispatcherServlet.java index 00c9da3e78..a3f320d677 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/DispatcherServlet.java @@ -1,4 +1,4 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; @@ -6,23 +6,27 @@ import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.handermapping.HandlerMappings; +import webmvc.org.springframework.web.servlet.mvc.handlerAdaptor.HandlerAdaptors; +import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; public class DispatcherServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); + private final String rootPackagePath; private HandlerAdaptors handlerAdaptors; private HandlerMappings handlerMappings; - public DispatcherServlet() { + public DispatcherServlet(final String rootPackagePath) { + this.rootPackagePath = rootPackagePath; } @Override public void init() { handlerAdaptors = new HandlerAdaptors(); - handlerMappings = new HandlerMappings(this.getClass().getPackageName()); + handlerMappings = new HandlerMappings(rootPackagePath); handlerMappings.initialize(); } diff --git a/app/src/main/java/com/techcourse/UncheckedServletException.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/UncheckedServletException.java similarity index 73% rename from app/src/main/java/com/techcourse/UncheckedServletException.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/UncheckedServletException.java index 26acea7605..0286b8c5aa 100644 --- a/app/src/main/java/com/techcourse/UncheckedServletException.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/UncheckedServletException.java @@ -1,4 +1,4 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; public class UncheckedServletException extends RuntimeException { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java deleted file mode 100644 index bdd1fde780..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java +++ /dev/null @@ -1,8 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -public interface Controller { - String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception; -} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java deleted file mode 100644 index cd8f1ef371..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java +++ /dev/null @@ -1,20 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -import java.util.Objects; - -public class ForwardController implements Controller { - - private final String path; - - public ForwardController(final String path) { - this.path = Objects.requireNonNull(path); - } - - @Override - public String execute(final HttpServletRequest request, final HttpServletResponse response) { - return path; - } -} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handermapping/AnnotationHandlerMapping.java similarity index 86% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handermapping/AnnotationHandlerMapping.java index c22c44518a..8be5b0f6b3 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handermapping/AnnotationHandlerMapping.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; +package webmvc.org.springframework.web.servlet.mvc.handermapping; import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; @@ -8,6 +8,7 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import org.reflections.Reflections; @@ -15,6 +16,8 @@ import org.slf4j.LoggerFactory; import web.org.springframework.web.bind.annotation.RequestMapping; import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.mvc.handlerAdaptor.HandlerExecution; +import webmvc.org.springframework.web.servlet.mvc.handlerAdaptor.HandlerKey; public class AnnotationHandlerMapping { @@ -80,6 +83,11 @@ public Object getHandler(final HttpServletRequest request) { final HandlerKey handlerKeyByRequest = new HandlerKey(request.getRequestURI(), RequestMethod.valueOf(request.getMethod())); - return handlerExecutions.get(handlerKeyByRequest); + final HandlerExecution handlerExecution = handlerExecutions.get(handlerKeyByRequest); + + if (Objects.isNull(handlerExecution)) { + return handlerExecutions.get(new HandlerKey("/404", RequestMethod.GET)); + } + return handlerExecution; } } diff --git a/app/src/main/java/com/techcourse/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handermapping/HandlerMappings.java similarity index 50% rename from app/src/main/java/com/techcourse/HandlerMappings.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handermapping/HandlerMappings.java index 1f618b9dc7..7e90669a10 100644 --- a/app/src/main/java/com/techcourse/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handermapping/HandlerMappings.java @@ -1,38 +1,27 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc.handermapping; import jakarta.servlet.http.HttpServletRequest; +import java.util.NoSuchElementException; import java.util.Objects; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; public class HandlerMappings { - private static final String NOT_FOUND_URI = "/404"; - - private final ManualHandlerMapping manualHandlerMapping; private final AnnotationHandlerMapping annotationHandlerMapping; public HandlerMappings(final String packageName) { - this.manualHandlerMapping = new ManualHandlerMapping(); this.annotationHandlerMapping = new AnnotationHandlerMapping(packageName); } public void initialize() { - manualHandlerMapping.initialize(); annotationHandlerMapping.initialize(); } public Object getHandler(final HttpServletRequest request) { - final Controller manualHandler = manualHandlerMapping.getHandler(request.getRequestURI()); - if (Objects.nonNull(manualHandler)) { - return manualHandler; - } - final Object annotationHandler = annotationHandlerMapping.getHandler(request); if (Objects.nonNull(annotationHandler)) { return annotationHandler; } - return manualHandlerMapping.getHandler(NOT_FOUND_URI); + throw new NoSuchElementException("핸들러를 찾을 수 없습니다."); } } diff --git a/app/src/main/java/com/techcourse/ExecutionHandlerAdaptor.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/ExecutionHandleradaptor.java similarity index 52% rename from app/src/main/java/com/techcourse/ExecutionHandlerAdaptor.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/ExecutionHandleradaptor.java index 17cf6e74c0..00d48e5261 100644 --- a/app/src/main/java/com/techcourse/ExecutionHandlerAdaptor.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/ExecutionHandleradaptor.java @@ -1,11 +1,9 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc.handlerAdaptor; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; -public class ExecutionHandlerAdaptor implements HandlerAdaptor { +public class ExecutionHandleradaptor implements Handleradaptor { @Override public boolean isSame(final Object handler) { @@ -13,8 +11,8 @@ public boolean isSame(final Object handler) { } @Override - public ModelAndView execute(final Object handler, final HttpServletRequest request, - final HttpServletResponse response) + public Object execute(final Object handler, final HttpServletRequest request, + final HttpServletResponse response) throws Exception { final HandlerExecution handlerExecution = (HandlerExecution) handler; return handlerExecution.handle(request, response); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerAdaptors.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerAdaptors.java new file mode 100644 index 0000000000..d76a357d0e --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerAdaptors.java @@ -0,0 +1,43 @@ +package webmvc.org.springframework.web.servlet.mvc.handlerAdaptor; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.List; +import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.view.JsonView; +import webmvc.org.springframework.web.servlet.mvc.view.JspView; + +public class HandlerAdaptors { + + private final List handleradaptors; + + public HandlerAdaptors() { + this.handleradaptors = List.of(new ExecutionHandleradaptor()); + } + + public ModelAndView execute(final Object handler, final HttpServletRequest request, + final HttpServletResponse response) throws Exception { + final Handleradaptor findHandleradaptor = findAdaptor(handler); + + final Object result = findHandleradaptor.execute(handler, request, response); + + if (result instanceof ModelAndView) { + return (ModelAndView) result; + } + + if (result instanceof String) { + return new ModelAndView(new JspView((String) result)); + } + + final ModelAndView modelAndView = new ModelAndView(new JsonView()); + modelAndView.addObject("model", result); + return modelAndView; + } + + private Handleradaptor findAdaptor(final Object handler) { + return handleradaptors.stream() + .filter(adaptor -> adaptor.isSame(handler)) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerExecution.java similarity index 66% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerExecution.java index a0c278cb92..d203c6152e 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerExecution.java @@ -1,9 +1,8 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; +package webmvc.org.springframework.web.servlet.mvc.handlerAdaptor; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.lang.reflect.Method; -import webmvc.org.springframework.web.servlet.ModelAndView; public class HandlerExecution { @@ -19,7 +18,7 @@ public static HandlerExecution of(final Class executor, final Method handlerM return new HandlerExecution(executor.getConstructor().newInstance(), handlerMethod); } - public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { - return (ModelAndView) handlerMethod.invoke(executor, request, response); + public Object handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { + return handlerMethod.invoke(executor, request, response); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerKey.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerKey.java similarity index 80% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerKey.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerKey.java index 30d3c780ff..301d5823b8 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerKey.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/HandlerKey.java @@ -1,8 +1,7 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; - -import web.org.springframework.web.bind.annotation.RequestMethod; +package webmvc.org.springframework.web.servlet.mvc.handlerAdaptor; import java.util.Objects; +import web.org.springframework.web.bind.annotation.RequestMethod; public class HandlerKey { @@ -24,8 +23,12 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof HandlerKey)) return false; + if (this == o) { + return true; + } + if (!(o instanceof HandlerKey)) { + return false; + } HandlerKey that = (HandlerKey) o; return Objects.equals(url, that.url) && requestMethod == that.requestMethod; } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/Handleradaptor.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/Handleradaptor.java new file mode 100644 index 0000000000..9807bc8b24 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handlerAdaptor/Handleradaptor.java @@ -0,0 +1,12 @@ +package webmvc.org.springframework.web.servlet.mvc.handlerAdaptor; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +public interface Handleradaptor { + + boolean isSame(final Object handler); + + Object execute(final Object handler, final HttpServletRequest request, final HttpServletResponse response) + throws Exception; +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/JsonView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/JsonView.java new file mode 100644 index 0000000000..1b019f56a0 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/JsonView.java @@ -0,0 +1,28 @@ +package webmvc.org.springframework.web.servlet.mvc.view; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.PrintWriter; +import java.util.Map; +import web.org.springframework.http.MediaType; + +public class JsonView implements View { + + private final ObjectMapper objectMapper; + + public JsonView() { + this.objectMapper = new ObjectMapper(); + } + + @Override + public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) + throws Exception { + + response.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE); + final String result = objectMapper.writeValueAsString(model.get("model")); + + final PrintWriter writer = response.getWriter(); + writer.write(result); + } +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/JspView.java similarity index 91% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/JspView.java index 0b34077a3e..f2c71d7abd 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/JspView.java @@ -1,11 +1,10 @@ -package webmvc.org.springframework.web.servlet.view; +package webmvc.org.springframework.web.servlet.mvc.view; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.View; public class JspView implements View { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/ModelAndView.java similarity index 92% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/ModelAndView.java index ff8e24553f..62d34276e0 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/ModelAndView.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet; +package webmvc.org.springframework.web.servlet.mvc.view; import java.util.Collections; import java.util.HashMap; diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/View.java similarity index 82% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/View.java index e3dcec2408..2e62dfdce2 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/View.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet; +package webmvc.org.springframework.web.servlet.mvc.view; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java deleted file mode 100644 index 522af7f911..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java +++ /dev/null @@ -1,14 +0,0 @@ -package webmvc.org.springframework.web.servlet.view; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.util.Map; -import webmvc.org.springframework.web.servlet.View; - -public class JsonView implements View { - - @Override - public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) - throws Exception { - } -} diff --git a/mvc/src/test/java/samples/TestController.java b/mvc/src/test/java/samples/TestController.java index 1f0e4acfb3..c8ce8c54bc 100644 --- a/mvc/src/test/java/samples/TestController.java +++ b/mvc/src/test/java/samples/TestController.java @@ -7,8 +7,8 @@ import org.slf4j.LoggerFactory; import web.org.springframework.web.bind.annotation.RequestMapping; import web.org.springframework.web.bind.annotation.RequestMethod; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; +import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.view.JspView; @Controller public class TestController { diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java index c6ae5b490f..6fec4c6306 100644 --- a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java @@ -8,6 +8,9 @@ import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import webmvc.org.springframework.web.servlet.mvc.handermapping.AnnotationHandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.handlerAdaptor.HandlerExecution; +import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; class AnnotationHandlerMappingTest { @@ -29,7 +32,7 @@ void get() throws Exception { when(request.getMethod()).thenReturn("GET"); final var handlerExecution = (HandlerExecution) handlerMapping.getHandler(request); - final var modelAndView = handlerExecution.handle(request, response); + final var modelAndView = (ModelAndView) handlerExecution.handle(request, response); assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); } @@ -44,7 +47,7 @@ void post() throws Exception { when(request.getMethod()).thenReturn("POST"); final var handlerExecution = (HandlerExecution) handlerMapping.getHandler(request); - final var modelAndView = handlerExecution.handle(request, response); + final var modelAndView = (ModelAndView) handlerExecution.handle(request, response); assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); } From c2c73de2063fcfe1586dd68005a401146807dd5f Mon Sep 17 00:00:00 2001 From: wugawuga Date: Tue, 26 Sep 2023 00:39:11 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20render=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/servlet/DispatcherServlet.java | 11 +---------- .../web/servlet/mvc/view/ModelAndView.java | 11 ++++------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/DispatcherServlet.java index a3f320d677..2db501f998 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/DispatcherServlet.java @@ -8,7 +8,6 @@ import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.mvc.handermapping.HandlerMappings; import webmvc.org.springframework.web.servlet.mvc.handlerAdaptor.HandlerAdaptors; -import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView; public class DispatcherServlet extends HttpServlet { @@ -39,18 +38,10 @@ protected void service(final HttpServletRequest request, final HttpServletRespon try { final var handler = handlerMappings.getHandler(request); final var modelAndView = handlerAdaptors.execute(handler, request, response); - render(modelAndView, request, response); + modelAndView.render(request, response); } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); throw new ServletException(e.getMessage()); } } - - private void render(final ModelAndView modelAndView, final HttpServletRequest request, - final HttpServletResponse response) throws Exception { - final var view = modelAndView.getView(); - final var model = modelAndView.getModel(); - - view.render(model, request, response); - } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/ModelAndView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/ModelAndView.java index 62d34276e0..acba23a1d5 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/ModelAndView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/view/ModelAndView.java @@ -1,6 +1,7 @@ package webmvc.org.springframework.web.servlet.mvc.view; -import java.util.Collections; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; @@ -23,11 +24,7 @@ public Object getObject(final String attributeName) { return model.get(attributeName); } - public Map getModel() { - return Collections.unmodifiableMap(model); - } - - public View getView() { - return view; + public void render(final HttpServletRequest request, final HttpServletResponse response) throws Exception { + view.render(model, request, response); } }