From 42f1a06fd37472f6d6a5436570a1b85d029e91b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=9C=EC=9D=B4=EB=AF=B8?= <63184334+JJ503@users.noreply.github.com> Date: Mon, 25 Sep 2023 23:16:26 +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=EC=A0=9C=EC=9D=B4=EB=AF=B8(=EC=9E=84?= =?UTF-8?q?=EC=A0=95=EC=88=98)=20=EB=AF=B8=EC=85=98=20=EC=A0=9C=EC=B6=9C?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4.=20(#593)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 충돌 문제 해결 * refactor: 코드 컨벤션 관련 수정 * fix: basePackage가 여러개여도 처리가 가능하도록 수정 * refactor: render를 ModelAndView 내부에서 수행하도록 수정 * feat: UserController 추가 - 요구 사항을 위해 추가 * feat: JsonView 반환 기능 추가 * refactor: 모든 컨트롤러를 너오테이션 기반 mvc로 변경 * refactor: 레거시 코드 제거 * rename: DispatcherServlet 패키지 이동 * feat: 에외 처리 추가 * fix: 충돌 문제 해결 --- .../DispatcherServletInitializer.java | 4 +- .../com/techcourse/ManualHandlerMapping.java | 41 ------------------- .../controller/LoginController.java | 39 ++++++++++++------ .../controller/LoginViewController.java | 22 ---------- .../controller/LogoutController.java | 18 +++++--- .../techcourse/controller/MainController.java | 18 ++++++++ .../controller/RegisterController.java | 12 +++--- .../techcourse/controller/UserController.java | 32 +++++++++++++++ .../web/servlet/ModelAndView.java | 12 ++++++ .../servlet/exception/RenderingException.java | 8 ++++ .../web/servlet/mvc}/DispatcherServlet.java | 28 ++++++++----- .../servlet/mvc/HandlerAdapterRegistry.java | 2 +- .../servlet/mvc/HandlerMappingRegistry.java | 2 +- .../web/servlet/mvc/asis/Controller.java | 8 ---- .../mvc/asis/ControllerHandlerAdapter.java | 23 ----------- .../servlet/mvc/asis/ForwardController.java | 20 --------- .../mvc/tobe/AnnotationHandlerMapping.java | 9 ++-- .../web/servlet/view/JsonView.java | 12 ++++++ 18 files changed, 153 insertions(+), 157 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/ManualHandlerMapping.java delete mode 100644 app/src/main/java/com/techcourse/controller/LoginViewController.java create mode 100644 app/src/main/java/com/techcourse/controller/MainController.java create mode 100644 app/src/main/java/com/techcourse/controller/UserController.java create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/exception/RenderingException.java rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/DispatcherServlet.java (58%) 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/ControllerHandlerAdapter.java delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 4052e5f834..9763049c55 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -4,9 +4,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.web.WebApplicationInitializer; +import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet; import webmvc.org.springframework.web.servlet.mvc.HandlerAdapterRegistry; import webmvc.org.springframework.web.servlet.mvc.HandlerMappingRegistry; -import webmvc.org.springframework.web.servlet.mvc.asis.ControllerHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionHandlerAdapter; @@ -42,7 +42,6 @@ public void onStartup(final ServletContext servletContext) { private static HandlerMappingRegistry initializedHandlerAdapater() { final HandlerMappingRegistry handlerMappingRegistry = new HandlerMappingRegistry(); handlerMappingRegistry.addHandlerMapping(new AnnotationHandlerMapping("com.techcourse.controller")); - handlerMappingRegistry.addHandlerMapping(new ManualHandlerMapping()); return handlerMappingRegistry; } @@ -50,7 +49,6 @@ private static HandlerMappingRegistry initializedHandlerAdapater() { private static HandlerAdapterRegistry initializedHandlerAdapterRegistry() { final HandlerAdapterRegistry handlerAdapterRegistry = new HandlerAdapterRegistry(); handlerAdapterRegistry.addHandlerAdapter(new HandlerExecutionHandlerAdapter()); - handlerAdapterRegistry.addHandlerAdapter(new ControllerHandlerAdapter()); return handlerAdapterRegistry; } 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 0bb73c3fb6..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,41 +0,0 @@ -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 webmvc.org.springframework.web.servlet.mvc.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 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()); - - log.info("Initialized Handler Mapping!"); - controllers.keySet() - .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); - } - - @Override - public Object getHandler(final HttpServletRequest request) { - final String requestURI = request.getRequestURI(); - log.debug("Request Mapping Uri : {}", requestURI); - - return controllers.getOrDefault(requestURI, new ForwardController("/404.jsp")); - } -} diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 7baefdac00..6be1f1ecbd 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,36 +2,51 @@ 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 { - if (UserSession.isLoggedIn(req.getSession())) { - return "redirect:/index.jsp"; + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception { + if (UserSession.isLoggedIn(request.getSession())) { + return new ModelAndView(new JspView("/index.jsp")); } - return InMemoryUserRepository.findByAccount(req.getParameter("account")) + return InMemoryUserRepository.findByAccount(request.getParameter("account")) .map(user -> { log.info("User : {}", user); - return login(req, user); + return login(request, user); }) - .orElse("redirect:/401.jsp"); + .orElse(new ModelAndView(new JspView("/401.jsp"))); } - 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(new JspView("/index.jsp")); } - return "redirect:/401.jsp"; + return new ModelAndView(new JspView("/401.jsp")); + } + + @RequestMapping(value = "/login/view", method = RequestMethod.GET) + public ModelAndView show(final HttpServletRequest request, final HttpServletResponse response) { + return UserSession.getUserFrom(request.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return new ModelAndView(new JspView("redirect:/index.jsp")); + }) + .orElse(new ModelAndView(new JspView("/login.jsp"))); } } 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..8f596be97d 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,15 +1,21 @@ 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.JspView; -public class LogoutController implements Controller { +@Controller +public class LogoutController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - final var session = req.getSession(); + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView logout(final HttpServletRequest request, final HttpServletResponse response) { + final var session = request.getSession(); session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; + + return new ModelAndView(new JspView("redirect:/")); } } diff --git a/app/src/main/java/com/techcourse/controller/MainController.java b/app/src/main/java/com/techcourse/controller/MainController.java new file mode 100644 index 0000000000..f998334299 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/MainController.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.JspView; + +@Controller +public class MainController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView show(final HttpServletRequest request, final HttpServletResponse response) throws Exception { + return new ModelAndView(new JspView("/index.jsp")); + } +} diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 2c9dde9def..3c076dcb56 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -14,18 +14,18 @@ public class RegisterController { @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView register(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public ModelAndView register(final HttpServletRequest request, final HttpServletResponse response) { final var user = new User(2, - req.getParameter("account"), - req.getParameter("password"), - req.getParameter("email")); + request.getParameter("account"), + request.getParameter("password"), + request.getParameter("email")); InMemoryUserRepository.save(user); - return new ModelAndView(new JspView("/index.jsp")); + return new ModelAndView(new JspView("redirect:/index.jsp")); } @RequestMapping(value = "/register/view", method = RequestMethod.GET) - public ModelAndView showRegister(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public ModelAndView show(final HttpServletRequest request, final HttpServletResponse response) { return new ModelAndView(new JspView("/register.jsp")); } } 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..9a2fdc5029 --- /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.debug("user id : {}", account); + + final ModelAndView modelAndView = new ModelAndView(new JsonView()); + final User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(); + + modelAndView.addObject("user", user); + return modelAndView; + } +} 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..cd722b695e 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,9 @@ package webmvc.org.springframework.web.servlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.exception.RenderingException; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -19,6 +23,14 @@ public ModelAndView addObject(final String attributeName, final Object attribute return this; } + public void render(final HttpServletRequest request, HttpServletResponse response) { + try { + view.render(model, request, response); + } catch (Exception e) { + throw new RenderingException("렌더링 중 문제가 발생했습니다."); + } + } + public Object getObject(final String attributeName) { return model.get(attributeName); } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/exception/RenderingException.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/exception/RenderingException.java new file mode 100644 index 0000000000..d5cf4c6f24 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/exception/RenderingException.java @@ -0,0 +1,8 @@ +package webmvc.org.springframework.web.servlet.exception; + +public class RenderingException extends IllegalArgumentException { + + public RenderingException(final String message) { + super(message); + } +} 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 58% 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 9a551e0266..d597911e96 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java @@ -1,15 +1,12 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; -import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; 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.HandlerAdapter; -import webmvc.org.springframework.web.servlet.mvc.HandlerAdapterRegistry; -import webmvc.org.springframework.web.servlet.mvc.HandlerMappingRegistry; +import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { @@ -30,18 +27,27 @@ public void init() { } @Override - protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { + protected void service(final HttpServletRequest request, final HttpServletResponse response) { final String requestURI = request.getRequestURI(); log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI); try { - final Object handle = handlerMappingRegistry.getHandler(request); - final HandlerAdapter handlerAdapter = handlerAdapterRegistry.getHandlerAdapter(handle); - final ModelAndView modelAndView = handlerAdapter.handle(handle, request, response); - modelAndView.getView().render(modelAndView.getModel(), request, response); + final var handle = handlerMappingRegistry.getHandler(request); + final var handlerAdapter = handlerAdapterRegistry.getHandlerAdapter(handle); + final var modelAndView = handlerAdapter.handle(handle, request, response); + modelAndView.render(request, response); + } catch (IllegalArgumentException e) { + log.error("Exception : {}", e.getMessage(), e); + + final var modelAndView = new ModelAndView(new JspView("/404.jsp")); + response.setStatus(404); + modelAndView.render(request, response); } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); - throw new ServletException(e.getMessage()); + + final var modelAndView = new ModelAndView(new JspView("/500.jsp")); + response.setStatus(500); + modelAndView.render(request, response); } } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapterRegistry.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapterRegistry.java index 66e0ee1bdd..91dc162a9a 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapterRegistry.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapterRegistry.java @@ -7,7 +7,7 @@ public class HandlerAdapterRegistry { - final private List handlerAdapters = new ArrayList<>(); + private final List handlerAdapters = new ArrayList<>(); public void addHandlerAdapter(final HandlerAdapter handlerAdapter) { handlerAdapters.add(handlerAdapter); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappingRegistry.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappingRegistry.java index d8e24aea75..8ce4709bf1 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappingRegistry.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappingRegistry.java @@ -9,7 +9,7 @@ public class HandlerMappingRegistry { - final private List handlerMappings = new ArrayList<>(); + private final List handlerMappings = new ArrayList<>(); public void initialize() { 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/ControllerHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java deleted file mode 100644 index 5ad7e4c78c..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java +++ /dev/null @@ -1,23 +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.HandlerAdapter; -import webmvc.org.springframework.web.servlet.view.JspView; - -public class ControllerHandlerAdapter implements HandlerAdapter { - - @Override - public boolean isSupport(final Object object) { - return object instanceof Controller; - } - - @Override - public ModelAndView handle(final Object object, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - final Controller controller = (Controller) object; - final String path = controller.execute(request, response); - - return new ModelAndView(new JspView(path)); - } -} 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/tobe/AnnotationHandlerMapping.java index 3a2b0de8ea..90cf6e3465 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/tobe/AnnotationHandlerMapping.java @@ -32,10 +32,13 @@ public AnnotationHandlerMapping(final Object... basePackage) { @Override public void initialize() { log.info("Initialized AnnotationHandlerMapping!"); - Reflections reflections = new Reflections(basePackage); - final Set> controllerClasses = reflections.getTypesAnnotatedWith(Controller.class); - controllerClasses.forEach(this::putHandlerExecutions); + for (Object base :basePackage){ + Reflections reflections = new Reflections(base); + + final Set> controllerClasses = reflections.getTypesAnnotatedWith(Controller.class); + controllerClasses.forEach(this::putHandlerExecutions); + } } private void putHandlerExecutions(final Class clazz) { 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..a769187577 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,26 @@ package webmvc.org.springframework.web.servlet.view; +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 final ObjectMapper objectMapper = new ObjectMapper(); + @Override public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + + if (model.isEmpty()) { + return; + } + + final String json = objectMapper.writeValueAsString(model); + response.getWriter().write(json); } }