From 3e5ae810dd8bc651e7d41ecb9d6a5158579527c1 Mon Sep 17 00:00:00 2001 From: This2sho Date: Mon, 25 Sep 2023 12:36:52 +0900 Subject: [PATCH 01/10] =?UTF-8?q?refactor:=20HandlerAdapter=EC=97=90?= =?UTF-8?q?=EC=84=9C=20ModelAndView=20=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/DispatcherServlet.java | 4 +++- .../servlet/mvc/asis/ManualHandlerAdapter.java | 15 ++++++++------- .../mvc/tobe/AnnotationHandlerAdapter.java | 10 +++------- .../web/servlet/mvc/tobe/HandlerAdapter.java | 3 ++- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index b98c107f6f..f167e901a3 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -6,6 +6,7 @@ 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.tobe.HandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapters; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappings; @@ -38,7 +39,8 @@ protected void service(final HttpServletRequest request, final HttpServletRespon final Object handler = handlerMappings.getHandler(request); final HandlerAdapter handlerAdapter = handlerAdapters.getHandlerAdapter(handler); - handlerAdapter.execute(request, response, handler); + final ModelAndView mav = handlerAdapter.execute(request, response, handler); + mav.render(request, response); } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); throw new ServletException(e.getMessage()); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ManualHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ManualHandlerAdapter.java index 66b787f72c..629a88e68b 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ManualHandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ManualHandlerAdapter.java @@ -2,6 +2,7 @@ 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.HandlerAdapter; import webmvc.org.springframework.web.servlet.view.JspView; @@ -13,16 +14,16 @@ public boolean supports(final Object handler) { } @Override - public void execute(final HttpServletRequest request, final HttpServletResponse response, final Object handler) + public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception { final Controller controller = (Controller) handler; final String viewName = controller.execute(request, response); - 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); + return toModelAndView(viewName); + } + + private ModelAndView toModelAndView(final String viewName) { + final JspView jspView = new JspView(viewName); + return new ModelAndView(jspView); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java index 109a90e61d..515243d662 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java @@ -2,9 +2,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.util.Map; import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.View; public class AnnotationHandlerAdapter implements HandlerAdapter { @@ -14,12 +12,10 @@ public boolean supports(final Object handler) { } @Override - public void execute(final HttpServletRequest request, final HttpServletResponse response, final Object handler) + public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response, + final Object handler) throws Exception { final HandlerExecution execution = (HandlerExecution) handler; - final ModelAndView mav = execution.handle(request, response); - final Map model = mav.getModel(); - final View view = mav.getView(); - view.render(model, request, response); + return execution.handle(request, response); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java index 698a6bcc4f..0add629d7c 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java @@ -2,10 +2,11 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; public interface HandlerAdapter { boolean supports(Object handler); - void execute(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; + ModelAndView execute(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; } From b91fe36fccfc21a56b12bf383850ec8c62c7b076 Mon Sep 17 00:00:00 2001 From: This2sho Date: Mon, 25 Sep 2023 12:38:30 +0900 Subject: [PATCH 02/10] =?UTF-8?q?refactor:=20ModelAndView=EC=97=90?= =?UTF-8?q?=EC=84=9C=20render=ED=95=B4=EC=A3=BC=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/servlet/ModelAndView.java | 10 +++++++ .../org/springframework/web/servlet/View.java | 4 +++ .../web/servlet/view/JsonView.java | 26 ++++++++++++++++--- .../web/servlet/view/JspView.java | 13 ++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) 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 ff8e24553f..081d881eac 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 @@ -1,5 +1,7 @@ package webmvc.org.springframework.web.servlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -30,4 +32,12 @@ public Map getModel() { public View getView() { return view; } + + public void render(HttpServletRequest request, HttpServletResponse response) throws Exception { + if (view.isRedirect()) { + response.sendRedirect(view.getViewName()); + return; + } + view.render(model, request, response); + } } 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 4499f36866..7c885ee374 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 @@ -7,4 +7,8 @@ public interface View { void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; + + boolean isRedirect(); + + 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 b42c3466f0..1a98f49d58 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 @@ -1,14 +1,34 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.View; - +import java.io.PrintWriter; import java.util.Map; +import web.org.springframework.http.MediaType; +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 { + public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) + throws Exception { + model.keySet().forEach(key -> request.setAttribute(key, model.get(key))); + final PrintWriter writer = response.getWriter(); + ObjectMapper objectMapper = new ObjectMapper(); + final String body = objectMapper.writeValueAsString(model); + + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + writer.write(body); + } + + @Override + public boolean isRedirect() { + return false; + } + + @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 215af935c0..d3b8294227 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 @@ -31,4 +31,17 @@ public void render(final Map model, final HttpServletRequest request, final RequestDispatcher requestDispatcher = request.getRequestDispatcher(viewName); requestDispatcher.forward(request, response); } + + @Override + public boolean isRedirect() { + return viewName.startsWith(REDIRECT_PREFIX); + } + + @Override + public String getViewName() { + if (isRedirect()) { + return viewName.substring(JspView.REDIRECT_PREFIX.length()); + } + return viewName; + } } From 8aecd672793795ee7b0cabfc5b2ab5899eda601f Mon Sep 17 00:00:00 2001 From: This2sho Date: Mon, 25 Sep 2023 12:38:48 +0900 Subject: [PATCH 03/10] =?UTF-8?q?refactor:=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=8A=A4=EC=BA=94=20=EB=94=94=EB=A0=89=ED=86=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=B6=95=EC=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/DispatcherServletInitializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index f0a9bd632f..52c5c9dde5 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -24,7 +24,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer { public void onStartup(final ServletContext servletContext) { final HandlerMappings handlerMappings = new HandlerMappings(); handlerMappings.add(new ManualHandlerMapping()); - handlerMappings.add(new AnnotationHandlerMapping()); + handlerMappings.add(new AnnotationHandlerMapping("com.techcourse.controller")); final HandlerAdapters handlerAdapters = new HandlerAdapters(); handlerAdapters.add(new ManualHandlerAdapter()); From 5f3e38eeee3552eb6f20164cfc03917fa6198ae0 Mon Sep 17 00:00:00 2001 From: This2sho Date: Mon, 25 Sep 2023 12:39:03 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feat:=20UserController=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/controller/UserController.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/src/main/java/com/techcourse/controller/UserController.java diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java new file mode 100644 index 0000000000..82ec5b578f --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -0,0 +1,32 @@ +package com.techcourse.controller; + +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 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.JsonView; + +@Controller +public class UserController { + + private static final Logger log = LoggerFactory.getLogger(UserController.class); + + @RequestMapping(value = "/api/user", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { + final String account = request.getParameter("account"); + log.info("user id : {}", account); + + final ModelAndView modelAndView = new ModelAndView(new JsonView()); + final User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(); + + modelAndView.addObject("user", user); + return modelAndView; + } +} From 71dcda1694166af591a9818b594537c5c9c8b117 Mon Sep 17 00:00:00 2001 From: This2sho Date: Mon, 25 Sep 2023 13:12:06 +0900 Subject: [PATCH 05/10] =?UTF-8?q?refactor:=20=EA=B8=B0=EC=A1=B4=20Controll?= =?UTF-8?q?er=20->=20AnnotationController=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/ManualHandlerMapping.java | 14 ++------ .../controller/ForwardController.java | 18 ++++++++++ .../controller/LoginController.java | 33 ++++++++++++++----- .../controller/LoginViewController.java | 22 ------------- .../controller/LogoutController.java | 15 ++++++--- .../controller/RegisterController.java | 6 ++-- .../controller/RegisterViewController.java | 13 -------- app/src/main/webapp/index.html | 2 +- app/src/main/webapp/index.jsp | 2 +- app/src/main/webapp/register.jsp | 2 +- .../servlet/mvc/asis/ForwardController.java | 20 ----------- .../web/servlet/view/JspViews.java | 23 +++++++++++++ 12 files changed, 83 insertions(+), 87 deletions(-) create mode 100644 app/src/main/java/com/techcourse/controller/ForwardController.java delete mode 100644 app/src/main/java/com/techcourse/controller/LoginViewController.java delete mode 100644 app/src/main/java/com/techcourse/controller/RegisterViewController.java delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspViews.java diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java index 9407a7843e..3419d78980 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/ManualHandlerMapping.java @@ -1,16 +1,11 @@ package com.techcourse; -import com.techcourse.controller.LoginController; -import com.techcourse.controller.LoginViewController; -import com.techcourse.controller.LogoutController; import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - 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; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; public class ManualHandlerMapping implements HandlerMapping { @@ -21,11 +16,6 @@ public class ManualHandlerMapping implements HandlerMapping { @Override 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()); - log.info("Initialized Handler Mapping!"); controllers.keySet() .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); diff --git a/app/src/main/java/com/techcourse/controller/ForwardController.java b/app/src/main/java/com/techcourse/controller/ForwardController.java new file mode 100644 index 0000000000..5a5fc008fe --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/ForwardController.java @@ -0,0 +1,18 @@ +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.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspViews; + +@Controller +public class ForwardController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) { + return new ModelAndView(JspViews.toIndexPage()); + } +} diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 0428fe109e..15fbd10baf 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,20 +2,35 @@ 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.JspViews; -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.GET) + public ModelAndView loginPage(final HttpServletRequest req, final HttpServletResponse res) { + return UserSession.getUserFrom(req.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return new ModelAndView(JspViews.toIndexPage()); + }) + .orElse(new ModelAndView(JspViews.toLoginPage())); + } + + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView login(final HttpServletRequest req, final HttpServletResponse res) { if (UserSession.isLoggedIn(req.getSession())) { - return "redirect:/index.jsp"; + return new ModelAndView(JspViews.toIndexPage()); } return InMemoryUserRepository.findByAccount(req.getParameter("account")) @@ -23,15 +38,15 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re log.info("User : {}", user); return login(req, user); }) - .orElse("redirect:/401.jsp"); + .orElse(new ModelAndView(JspViews.to401Page())); } - private String login(final HttpServletRequest request, final User user) { + private ModelAndView login(final HttpServletRequest request, final User user) { if (user.checkPassword(request.getParameter("password"))) { final var session = request.getSession(); session.setAttribute(UserSession.SESSION_KEY, user); - return "redirect:/index.jsp"; + return new ModelAndView(JspViews.toIndexPage()); } - return "redirect:/401.jsp"; + return new ModelAndView(JspViews.to401Page()); } } 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 index 4642fd9450..6e7a0274f1 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,15 +1,20 @@ package com.techcourse.controller; +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 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.JspViews; -public class LogoutController implements Controller { +@Controller +public class LogoutController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) { final var session = req.getSession(); session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; + return new ModelAndView(JspViews.toIndexPage()); } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 8a6164dadb..e4e04a7e28 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -8,7 +8,7 @@ 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.view.JspViews; @Controller public class RegisterController { @@ -21,11 +21,11 @@ public ModelAndView save(HttpServletRequest req, HttpServletResponse res) { req.getParameter("email")); InMemoryUserRepository.save(user); - return new ModelAndView(new JspView("/index.jsp")); + return new ModelAndView(JspViews.toIndexPage()); } @RequestMapping(value = "/register", method = RequestMethod.GET) public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { - return new ModelAndView(new JspView("/register.jsp")); + return new ModelAndView(JspViews.toRegisterPage()); } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java deleted file mode 100644 index 136962136d..0000000000 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ /dev/null @@ -1,13 +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 RegisterViewController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - return "/register.jsp"; - } -} diff --git a/app/src/main/webapp/index.html b/app/src/main/webapp/index.html index 95df361069..1ca98f1be2 100644 --- a/app/src/main/webapp/index.html +++ b/app/src/main/webapp/index.html @@ -19,7 +19,7 @@ diff --git a/app/src/main/webapp/index.jsp b/app/src/main/webapp/index.jsp index 142ee3d104..4ee84d2926 100644 --- a/app/src/main/webapp/index.jsp +++ b/app/src/main/webapp/index.jsp @@ -26,7 +26,7 @@ <% } else { %> <% } %> diff --git a/app/src/main/webapp/register.jsp b/app/src/main/webapp/register.jsp index 7e68995a56..b42e5606be 100644 --- a/app/src/main/webapp/register.jsp +++ b/app/src/main/webapp/register.jsp @@ -36,7 +36,7 @@ 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/view/JspViews.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspViews.java new file mode 100644 index 0000000000..d1aa9de7f8 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspViews.java @@ -0,0 +1,23 @@ +package webmvc.org.springframework.web.servlet.view; + +public class JspViews { + + private JspViews() { + } + + public static JspView toIndexPage() { + return new JspView("/index.jsp"); + } + + public static JspView toLoginPage() { + return new JspView("/login.jsp"); + } + + public static JspView to401Page() { + return new JspView("/401.jsp"); + } + + public static JspView toRegisterPage() { + return new JspView("/register.jsp"); + } +} From 8caebe14e968c21a7fc03e1bede7d671f96a9e02 Mon Sep 17 00:00:00 2001 From: This2sho Date: Mon, 25 Sep 2023 13:33:27 +0900 Subject: [PATCH 06/10] =?UTF-8?q?refactor:=20legacy=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20DispatcherServlet=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DispatcherServletInitializer.java | 8 ++--- .../com/techcourse/ManualHandlerMapping.java | 29 ------------------- .../web/servlet/mvc}/DispatcherServlet.java | 2 +- .../web/servlet/mvc/asis/Controller.java | 8 ----- .../mvc/asis/ManualHandlerAdapter.java | 29 ------------------- 5 files changed, 4 insertions(+), 72 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/ManualHandlerMapping.java rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/DispatcherServlet.java (97%) 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/ManualHandlerAdapter.java diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 52c5c9dde5..f2dfe8797d 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -4,15 +4,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.web.WebApplicationInitializer; -import webmvc.org.springframework.web.servlet.mvc.asis.ManualHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapters; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappings; /** - * 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 { @@ -23,11 +23,9 @@ public class DispatcherServletInitializer implements WebApplicationInitializer { @Override public void onStartup(final ServletContext servletContext) { final HandlerMappings handlerMappings = new HandlerMappings(); - handlerMappings.add(new ManualHandlerMapping()); handlerMappings.add(new AnnotationHandlerMapping("com.techcourse.controller")); final HandlerAdapters handlerAdapters = new HandlerAdapters(); - handlerAdapters.add(new ManualHandlerAdapter()); handlerAdapters.add(new AnnotationHandlerAdapter()); final var dispatcherServlet = new DispatcherServlet(handlerMappings, handlerAdapters); 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 3419d78980..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.techcourse; - -import jakarta.servlet.http.HttpServletRequest; -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.tobe.HandlerMapping; - -public class ManualHandlerMapping implements HandlerMapping { - - private static final Logger log = LoggerFactory.getLogger(ManualHandlerMapping.class); - - private static final Map controllers = new HashMap<>(); - - @Override - public void initialize() { - log.info("Initialized Handler Mapping!"); - controllers.keySet() - .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); - } - - @Override - public Controller getHandler(final HttpServletRequest request) { - log.debug("Request Mapping Uri : {}", request.getRequestURI()); - return controllers.get(request.getRequestURI()); - } -} diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java similarity index 97% rename from app/src/main/java/com/techcourse/DispatcherServlet.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java index f167e901a3..2d3c834767 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java @@ -1,4 +1,4 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; 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/ManualHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ManualHandlerAdapter.java deleted file mode 100644 index 629a88e68b..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ManualHandlerAdapter.java +++ /dev/null @@ -1,29 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -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.HandlerAdapter; -import webmvc.org.springframework.web.servlet.view.JspView; - -public class ManualHandlerAdapter implements HandlerAdapter { - - @Override - public boolean supports(final Object handler) { - return handler instanceof Controller; - } - - @Override - public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response, final Object handler) - throws Exception { - final Controller controller = (Controller) handler; - final String viewName = controller.execute(request, response); - - return toModelAndView(viewName); - } - - private ModelAndView toModelAndView(final String viewName) { - final JspView jspView = new JspView(viewName); - return new ModelAndView(jspView); - } -} From 9320947c9a58ea7cffa6b9e42606f902f80d44bb Mon Sep 17 00:00:00 2001 From: This2sho Date: Mon, 25 Sep 2023 14:38:21 +0900 Subject: [PATCH 07/10] =?UTF-8?q?refactor:=20model=EC=9D=B4=20=ED=95=98?= =?UTF-8?q?=EB=82=98=EC=9D=BC=20=EB=95=8C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/servlet/view/JsonView.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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 1a98f49d58..ac77d70d42 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 @@ -1,5 +1,6 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -10,16 +11,24 @@ public class JsonView implements View { + private final ObjectMapper mapper = new ObjectMapper(); + @Override public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { model.keySet().forEach(key -> request.setAttribute(key, model.get(key))); final PrintWriter writer = response.getWriter(); - ObjectMapper objectMapper = new ObjectMapper(); - final String body = objectMapper.writeValueAsString(model); - + final String jsonResult = toResultBody(model); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); - writer.write(body); + writer.write(jsonResult); + } + + private String toResultBody(final Map model) throws JsonProcessingException { + if (model.size() == 1) { + final Object value = model.values().toArray()[0]; + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); + } + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model); } @Override From 48c3e562ad82c14a9916cc7941e4c5943f716b44 Mon Sep 17 00:00:00 2001 From: This2sho Date: Tue, 26 Sep 2023 03:38:54 +0900 Subject: [PATCH 08/10] =?UTF-8?q?refactor:=20=ED=95=98=EB=93=9C=20?= =?UTF-8?q?=EC=BD=94=EB=94=A9=20=EB=90=9C=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/DispatcherServletInitializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index f2dfe8797d..f41c6f4c14 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -23,7 +23,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer { @Override public void onStartup(final ServletContext servletContext) { final HandlerMappings handlerMappings = new HandlerMappings(); - handlerMappings.add(new AnnotationHandlerMapping("com.techcourse.controller")); + handlerMappings.add(new AnnotationHandlerMapping(this.getClass().getPackageName())); final HandlerAdapters handlerAdapters = new HandlerAdapters(); handlerAdapters.add(new AnnotationHandlerAdapter()); From 5a5cf03374df0160f3598a7d49683b4d3eac2974 Mon Sep 17 00:00:00 2001 From: This2sho Date: Tue, 26 Sep 2023 03:42:46 +0900 Subject: [PATCH 09/10] =?UTF-8?q?refactor:=20JspViews=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 양방향 의존... --- .../java/com/techcourse/controller/ForwardController.java | 1 - .../src/main/java/com/techcourse/controller}/JspViews.java | 4 +++- .../main/java/com/techcourse/controller/LoginController.java | 1 - .../main/java/com/techcourse/controller/LogoutController.java | 1 - .../java/com/techcourse/controller/RegisterController.java | 1 - 5 files changed, 3 insertions(+), 5 deletions(-) rename {mvc/src/main/java/webmvc/org/springframework/web/servlet/view => app/src/main/java/com/techcourse/controller}/JspViews.java (81%) diff --git a/app/src/main/java/com/techcourse/controller/ForwardController.java b/app/src/main/java/com/techcourse/controller/ForwardController.java index 5a5fc008fe..f40f2cddbd 100644 --- a/app/src/main/java/com/techcourse/controller/ForwardController.java +++ b/app/src/main/java/com/techcourse/controller/ForwardController.java @@ -6,7 +6,6 @@ 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.JspViews; @Controller public class ForwardController { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspViews.java b/app/src/main/java/com/techcourse/controller/JspViews.java similarity index 81% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspViews.java rename to app/src/main/java/com/techcourse/controller/JspViews.java index d1aa9de7f8..31d1441852 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspViews.java +++ b/app/src/main/java/com/techcourse/controller/JspViews.java @@ -1,4 +1,6 @@ -package webmvc.org.springframework.web.servlet.view; +package com.techcourse.controller; + +import webmvc.org.springframework.web.servlet.view.JspView; public class JspViews { diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 15fbd10baf..94c72c0867 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -10,7 +10,6 @@ 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.JspViews; @Controller public class LoginController { diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 6e7a0274f1..7175e95d5e 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -6,7 +6,6 @@ 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.JspViews; @Controller public class LogoutController { diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index e4e04a7e28..ee68ee3a59 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -8,7 +8,6 @@ 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.JspViews; @Controller public class RegisterController { From f28f2e975110eac44f0dda4c0a6864b2921caaf6 Mon Sep 17 00:00:00 2001 From: This2sho Date: Tue, 26 Sep 2023 03:48:08 +0900 Subject: [PATCH 10/10] =?UTF-8?q?refactor:=20ObjectMapper=20static?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webmvc/org/springframework/web/servlet/view/JsonView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ac77d70d42..cc7d31e542 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,7 +11,7 @@ public class JsonView implements View { - private final ObjectMapper mapper = new ObjectMapper(); + private static final ObjectMapper mapper = new ObjectMapper(); @Override public void render(final Map model, final HttpServletRequest request, HttpServletResponse response)