From 73baf4422e57c3732d316917764f24e4e6ab4d04 Mon Sep 17 00:00:00 2001 From: iamjooon2 Date: Tue, 26 Sep 2023 00:07:40 +0900 Subject: [PATCH] =?UTF-8?q?[MVC=20=EA=B5=AC=ED=98=84=ED=95=98=EA=B8=B0=20-?= =?UTF-8?q?=203=EB=8B=A8=EA=B3=84]=20=EA=B0=80=EB=B9=84=20=EB=AF=B8?= =?UTF-8?q?=EC=85=98=20=EC=A0=9C=EC=B6=9C=ED=95=A9=EB=8B=88=EB=8B=A4.=20?= =?UTF-8?q?=20(#609)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: JSONView 작성 * feat: Legacy MVC 제거 * refactor: 매직넘버 추출 * chore: 개행 제거 * refactor: 변수명 변경 * refactor: API 명세 기존과 맞도록 통일 * refactor: model에 데이터가 하나일 경우 값을 그대로 출력하도록 변경 * refactor: @Controller 내 경로 명세 제거 * refactor: logout 내 view 제거 --- .../DispatcherServletInitializer.java | 1 + .../com/techcourse/ManualHandlerMapping.java | 44 --------------- .../com/techcourse/ManulHandlerAdapter.java | 25 --------- .../techcourse/controller/HomeController.java | 24 ++++++++ .../controller/LoginController.java | 55 +++++++++++++++++++ .../controller/LogoutController.java | 21 +++++++ .../controller/RegisterController.java | 32 +++++++++++ .../{annotation => }/TestController.java | 2 +- .../techcourse/controller/UserController.java | 34 ++++++++++++ .../controller/{legacy => }/UserSession.java | 6 +- .../controller/legacy/LoginController.java | 37 ------------- .../legacy/LoginViewController.java | 22 -------- .../controller/legacy/LogoutController.java | 15 ----- .../controller/legacy/RegisterController.java | 21 ------- .../legacy/RegisterViewController.java | 13 ----- .../{tobe => }/AnnotationHandlerAdapter.java | 2 +- .../{tobe => }/AnnotationHandlerMapping.java | 2 +- .../web/servlet/mvc}/DispatcherServlet.java | 3 +- .../web/servlet/mvc}/HandlerAdapters.java | 5 +- .../mvc/{tobe => }/HandlerExecution.java | 2 +- .../servlet/mvc/{tobe => }/HandlerKey.java | 2 +- .../web/servlet/mvc}/HandlerMappings.java | 7 +-- .../web/servlet/mvc/asis/Controller.java | 8 --- .../servlet/mvc/asis/ForwardController.java | 20 ------- .../web/servlet/view/JsonView.java | 28 +++++++++- .../AnnotationHandlerMappingTest.java | 2 +- 26 files changed, 208 insertions(+), 225 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/ManualHandlerMapping.java delete mode 100644 app/src/main/java/com/techcourse/ManulHandlerAdapter.java create mode 100644 app/src/main/java/com/techcourse/controller/HomeController.java create mode 100644 app/src/main/java/com/techcourse/controller/LoginController.java create mode 100644 app/src/main/java/com/techcourse/controller/LogoutController.java create mode 100644 app/src/main/java/com/techcourse/controller/RegisterController.java rename app/src/main/java/com/techcourse/controller/{annotation => }/TestController.java (96%) create mode 100644 app/src/main/java/com/techcourse/controller/UserController.java rename app/src/main/java/com/techcourse/controller/{legacy => }/UserSession.java (87%) delete mode 100644 app/src/main/java/com/techcourse/controller/legacy/LoginController.java delete mode 100644 app/src/main/java/com/techcourse/controller/legacy/LoginViewController.java delete mode 100644 app/src/main/java/com/techcourse/controller/legacy/LogoutController.java delete mode 100644 app/src/main/java/com/techcourse/controller/legacy/RegisterController.java delete mode 100644 app/src/main/java/com/techcourse/controller/legacy/RegisterViewController.java rename mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/{tobe => }/AnnotationHandlerAdapter.java (92%) rename mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/{tobe => }/AnnotationHandlerMapping.java (98%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/DispatcherServlet.java (95%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/HandlerAdapters.java (81%) rename mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/{tobe => }/HandlerExecution.java (92%) rename mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/{tobe => }/HandlerKey.java (94%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/HandlerMappings.java (76%) 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/test/java/webmvc/org/springframework/web/servlet/mvc/{tobe => }/AnnotationHandlerMappingTest.java (96%) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 6e814cdd25..53fb9af5c8 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.web.WebApplicationInitializer; +import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet; /** * Base class for {@link WebApplicationInitializer} 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 e54ac71221..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.techcourse; - -import com.techcourse.controller.legacy.LoginController; -import com.techcourse.controller.legacy.LoginViewController; -import com.techcourse.controller.legacy.LogoutController; -import com.techcourse.controller.legacy.RegisterController; -import com.techcourse.controller.legacy.RegisterViewController; -import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.HandlerMapping; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; - -import java.util.HashMap; -import java.util.Map; - -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() { - controllers.put("/", new ForwardController("/index.jsp")); - controllers.put("/login", new LoginController()); - controllers.put("/login/view", new LoginViewController()); - controllers.put("/logout", new LogoutController()); - controllers.put("/register/view", new RegisterViewController()); - controllers.put("/register", new RegisterController()); - - 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) { - String requestURI = request.getRequestURI(); - log.debug("Request Mapping Uri : {}", requestURI); - return controllers.get(requestURI); - } -} diff --git a/app/src/main/java/com/techcourse/ManulHandlerAdapter.java b/app/src/main/java/com/techcourse/ManulHandlerAdapter.java deleted file mode 100644 index 2ceb38671a..0000000000 --- a/app/src/main/java/com/techcourse/ManulHandlerAdapter.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.techcourse; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.HandlerAdapter; -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 ManulHandlerAdapter implements HandlerAdapter { - - @Override - public boolean supports(Object handler) { - return Controller.class.isInstance(handler); - } - - @Override - public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - Controller controller = (Controller) handler; - String viewName = controller.execute(request, response); - - return new ModelAndView(new JspView(viewName)); - } - -} diff --git a/app/src/main/java/com/techcourse/controller/HomeController.java b/app/src/main/java/com/techcourse/controller/HomeController.java new file mode 100644 index 0000000000..9141ebaca0 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/HomeController.java @@ -0,0 +1,24 @@ +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.JspView; + +@Controller +public class HomeController { + + @RequestMapping(value = "/index", method = RequestMethod.GET) + public ModelAndView showIndex(HttpServletRequest request, HttpServletResponse response) { + return new ModelAndView(new JspView("index.jsp")); + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { + return new ModelAndView(new JspView("index.jsp")); + } + +} diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java new file mode 100644 index 0000000000..cabbeb7441 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -0,0 +1,55 @@ +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.JspView; + +@Controller +public class LoginController { + private static final String REDIRECT_HOME = "redirect:/index.jsp"; + private static final String REDIRECT_UNAUTHORIZED = "redirect:/401.jsp"; + + private static final Logger log = LoggerFactory.getLogger(LoginController.class); + + @RequestMapping(value = "/login/view", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { + return UserSession.getUserFrom(request.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return new ModelAndView(new JspView(REDIRECT_HOME)); + }) + .orElse(new ModelAndView(new JspView("/login.jsp"))); + } + + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView login(HttpServletRequest request, HttpServletResponse response) { + if (UserSession.isLoggedIn(request.getSession())) { + return new ModelAndView(new JspView(REDIRECT_HOME)); + } + + return InMemoryUserRepository.findByAccount(request.getParameter("account")) + .map(user -> { + log.info("User : {}", user); + return doLogin(request, user); + }) + .orElse(new ModelAndView(new JspView(REDIRECT_UNAUTHORIZED))); + } + + private ModelAndView doLogin(HttpServletRequest request, User user) { + if (user.checkPassword(request.getParameter("password"))) { + final var session = request.getSession(); + session.setAttribute(UserSession.SESSION_KEY, user); + return new ModelAndView(new JspView(REDIRECT_HOME)); + } else { + return new ModelAndView(new JspView(REDIRECT_UNAUTHORIZED)); + } + } +} diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java new file mode 100644 index 0000000000..6173758bd0 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -0,0 +1,21 @@ +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.JspView; + +@Controller +public class LogoutController { + + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { + final var session = request.getSession(); + session.removeAttribute(UserSession.SESSION_KEY); + return new ModelAndView(new JspView("redirect:/")); + } + +} diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java new file mode 100644 index 0000000000..0ae747b5dc --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/RegisterController.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 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; + +@Controller +public class RegisterController { + + @RequestMapping(value = "/register", method = RequestMethod.POST) + public ModelAndView register(HttpServletRequest req, HttpServletResponse res) { + final var user = new User(2, + req.getParameter("account"), + req.getParameter("password"), + req.getParameter("email")); + InMemoryUserRepository.save(user); + + return new ModelAndView(new JspView("redirect:/index.jsp")); + } + + @RequestMapping(value = "/register/view", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { + return new ModelAndView(new JspView("/register.jsp")); + } + +} diff --git a/app/src/main/java/com/techcourse/controller/annotation/TestController.java b/app/src/main/java/com/techcourse/controller/TestController.java similarity index 96% rename from app/src/main/java/com/techcourse/controller/annotation/TestController.java rename to app/src/main/java/com/techcourse/controller/TestController.java index 94474dd301..89f15a366d 100644 --- a/app/src/main/java/com/techcourse/controller/annotation/TestController.java +++ b/app/src/main/java/com/techcourse/controller/TestController.java @@ -1,4 +1,4 @@ -package com.techcourse.controller.annotation; +package com.techcourse.controller; import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; 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..bb1e95bd90 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -0,0 +1,34 @@ +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) { + String account = request.getParameter("account"); + log.debug("user id : {}", account); + + ModelAndView modelAndView = new ModelAndView(new JsonView()); + User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(); + + modelAndView.addObject("user", user); + return modelAndView; + } + +} diff --git a/app/src/main/java/com/techcourse/controller/legacy/UserSession.java b/app/src/main/java/com/techcourse/controller/UserSession.java similarity index 87% rename from app/src/main/java/com/techcourse/controller/legacy/UserSession.java rename to app/src/main/java/com/techcourse/controller/UserSession.java index cdfabec2dd..0e55c942a8 100644 --- a/app/src/main/java/com/techcourse/controller/legacy/UserSession.java +++ b/app/src/main/java/com/techcourse/controller/UserSession.java @@ -1,4 +1,4 @@ -package com.techcourse.controller.legacy; +package com.techcourse.controller; import com.techcourse.domain.User; import jakarta.servlet.http.HttpSession; @@ -9,6 +9,9 @@ public class UserSession { public static final String SESSION_KEY = "user"; + private UserSession() { + } + public static Optional getUserFrom(final HttpSession session) { final var user = (User) session.getAttribute(SESSION_KEY); return Optional.ofNullable(user); @@ -18,5 +21,4 @@ public static boolean isLoggedIn(final HttpSession session) { return getUserFrom(session).isPresent(); } - private UserSession() {} } diff --git a/app/src/main/java/com/techcourse/controller/legacy/LoginController.java b/app/src/main/java/com/techcourse/controller/legacy/LoginController.java deleted file mode 100644 index 0facc49030..0000000000 --- a/app/src/main/java/com/techcourse/controller/legacy/LoginController.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.techcourse.controller.legacy; - -import com.techcourse.domain.User; -import com.techcourse.repository.InMemoryUserRepository; -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; - -public class LoginController implements Controller { - - private static final Logger log = LoggerFactory.getLogger(LoginController.class); - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - if (UserSession.isLoggedIn(req.getSession())) { - return "redirect:/index.jsp"; - } - - return InMemoryUserRepository.findByAccount(req.getParameter("account")) - .map(user -> { - log.info("User : {}", user); - return login(req, user); - }) - .orElse("redirect:/401.jsp"); - } - - private String 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 "redirect:/401.jsp"; - } -} diff --git a/app/src/main/java/com/techcourse/controller/legacy/LoginViewController.java b/app/src/main/java/com/techcourse/controller/legacy/LoginViewController.java deleted file mode 100644 index bdb4825b97..0000000000 --- a/app/src/main/java/com/techcourse/controller/legacy/LoginViewController.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.techcourse.controller.legacy; - -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/legacy/LogoutController.java b/app/src/main/java/com/techcourse/controller/legacy/LogoutController.java deleted file mode 100644 index 7c150ea05f..0000000000 --- a/app/src/main/java/com/techcourse/controller/legacy/LogoutController.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.techcourse.controller.legacy; - -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/app/src/main/java/com/techcourse/controller/legacy/RegisterController.java b/app/src/main/java/com/techcourse/controller/legacy/RegisterController.java deleted file mode 100644 index fa282abdf6..0000000000 --- a/app/src/main/java/com/techcourse/controller/legacy/RegisterController.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.techcourse.controller.legacy; - -import com.techcourse.domain.User; -import com.techcourse.repository.InMemoryUserRepository; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; - -public class RegisterController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - final var user = new User(2, - req.getParameter("account"), - req.getParameter("password"), - req.getParameter("email")); - InMemoryUserRepository.save(user); - - return "redirect:/index.jsp"; - } -} diff --git a/app/src/main/java/com/techcourse/controller/legacy/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/legacy/RegisterViewController.java deleted file mode 100644 index 370347a4dc..0000000000 --- a/app/src/main/java/com/techcourse/controller/legacy/RegisterViewController.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.techcourse.controller.legacy; - -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/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/AnnotationHandlerAdapter.java similarity index 92% rename from mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/AnnotationHandlerAdapter.java index ec30aae64d..9f0591c32e 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/AnnotationHandlerAdapter.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; +package webmvc.org.springframework.web.servlet.mvc; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; 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/AnnotationHandlerMapping.java similarity index 98% 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/AnnotationHandlerMapping.java index 1e7aed35b3..10ffa3b082 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/AnnotationHandlerMapping.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; +package webmvc.org.springframework.web.servlet.mvc; import java.lang.reflect.Method; import java.util.Arrays; 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 95% 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 3271ae4236..9c404f0ebb 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; @@ -11,7 +11,6 @@ public class DispatcherServlet extends HttpServlet { - private static final long serialVersionUID = 1L; private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); private final HandlerMappings handlerMappings = new HandlerMappings(); diff --git a/app/src/main/java/com/techcourse/HandlerAdapters.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapters.java similarity index 81% rename from app/src/main/java/com/techcourse/HandlerAdapters.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapters.java index ed763367f8..017cd1e5d7 100644 --- a/app/src/main/java/com/techcourse/HandlerAdapters.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapters.java @@ -1,17 +1,16 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; import java.util.ArrayList; import java.util.List; import webmvc.org.springframework.web.servlet.HandlerAdapter; -import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.AnnotationHandlerAdapter; public class HandlerAdapters { private final List handlerAdapters = new ArrayList<>(); public HandlerAdapters() { - handlerAdapters.add(new ManulHandlerAdapter()); handlerAdapters.add(new AnnotationHandlerAdapter()); } 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/HandlerExecution.java similarity index 92% 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/HandlerExecution.java index 9839b1078d..4f61181021 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/HandlerExecution.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; +package webmvc.org.springframework.web.servlet.mvc; import java.lang.reflect.Method; 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/HandlerKey.java similarity index 94% 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/HandlerKey.java index 30d3c780ff..223763253d 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/HandlerKey.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; +package webmvc.org.springframework.web.servlet.mvc; import web.org.springframework.web.bind.annotation.RequestMethod; diff --git a/app/src/main/java/com/techcourse/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java similarity index 76% rename from app/src/main/java/com/techcourse/HandlerMappings.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java index f3ebdd7d5d..0786a12a93 100644 --- a/app/src/main/java/com/techcourse/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java @@ -1,4 +1,4 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; import java.util.ArrayList; import java.util.List; @@ -6,15 +6,14 @@ import jakarta.servlet.http.HttpServletRequest; import webmvc.org.springframework.web.servlet.HandlerMapping; -import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.AnnotationHandlerMapping; public class HandlerMappings { private final List handlerMappings = new ArrayList<>(); public void initialize() { - handlerMappings.add(new ManualHandlerMapping()); - handlerMappings.add(new AnnotationHandlerMapping("com.techcourse.controller")); + handlerMappings.add(new AnnotationHandlerMapping("com")); handlerMappings.forEach(HandlerMapping::initialize); } 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/view/JsonView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java index b42c3466f0..0b44762dcb 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,36 @@ package webmvc.org.springframework.web.servlet.view; +import java.io.PrintWriter; +import java.util.Map; + +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; -import java.util.Map; - public class JsonView implements View { + private static final int SINGLE_JSON_SIZE = 1; + private static final int SINGLE_JSON_FIRST_PARAM = 0; + + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + @Override - public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + PrintWriter printWriter = response.getWriter(); + String json = objectMapper.writeValueAsString(createResult(model)); + printWriter.write(json); } + + private Object createResult(Map model) { + if (model.size() == SINGLE_JSON_SIZE) { + return model.values().toArray()[SINGLE_JSON_FIRST_PARAM].toString(); + } + return model; + } + } 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/AnnotationHandlerMappingTest.java similarity index 96% rename from mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java rename to mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/AnnotationHandlerMappingTest.java index dcec215a3f..669d3485ee 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/AnnotationHandlerMappingTest.java @@ -1,4 +1,4 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; +package webmvc.org.springframework.web.servlet.mvc; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;