From 79f9f21d424034fc28005659cb7322b975df0a71 Mon Sep 17 00:00:00 2001 From: LJW25 Date: Sat, 23 Sep 2023 14:31:33 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor(HandlerMappings):=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=89=EC=BB=AC=EB=A0=89=EC=85=98=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/servlet/DispatcherServlet.java | 22 ++++--------- .../mvc/handler/AnnotationHandlerMapping.java | 3 -- .../servlet/mvc/handler/HandlerMappings.java | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerMappings.java diff --git a/app/src/main/java/com/techcourse/servlet/DispatcherServlet.java b/app/src/main/java/com/techcourse/servlet/DispatcherServlet.java index 91efd2ae99..5fb15658a2 100644 --- a/app/src/main/java/com/techcourse/servlet/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/servlet/DispatcherServlet.java @@ -4,8 +4,6 @@ import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.util.ArrayList; -import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.http.exception.MethodNotAllowedException; @@ -14,7 +12,7 @@ import webmvc.org.springframework.web.servlet.mvc.handler.HandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.handler.HandlerExecution; import webmvc.org.springframework.web.servlet.mvc.handler.HandlerExecutionHandlerAdapter; -import webmvc.org.springframework.web.servlet.mvc.handler.HandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.handler.HandlerMappings; import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { @@ -22,16 +20,16 @@ public class DispatcherServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); - private final List handlerMappings = new ArrayList<>(); + private final HandlerMappings handlerMappings = new HandlerMappings(); public DispatcherServlet() { } @Override public void init() { - handlerMappings.add(new ManualHandlerMapping()); - handlerMappings.add(new AnnotationHandlerMapping("com.techcourse")); - handlerMappings.forEach(HandlerMapping::initialize); + handlerMappings.addHandlerMapping(new ManualHandlerMapping()); + handlerMappings.addHandlerMapping(new AnnotationHandlerMapping("com.techcourse")); + handlerMappings.init(); } @Override @@ -40,7 +38,7 @@ protected void service(final HttpServletRequest request, final HttpServletRespon log.debug("Method : {}, Request URI : {}", request.getMethod(), request.getRequestURI()); try { - final Object handler = mapHandler(request); + final Object handler = handlerMappings.mapHandler(request); final HandlerAdapter handlerAdapter = mapHandlerAdapter(handler); final Object view = handlerAdapter.execute(request, response); @@ -52,14 +50,6 @@ protected void service(final HttpServletRequest request, final HttpServletRespon } } - private Object mapHandler(final HttpServletRequest request) { - return handlerMappings.stream() - .filter(handlerMapping -> handlerMapping.getHandler(request) != null) - .findFirst() - .orElseThrow(() -> new MethodNotAllowedException(request.getMethod())) - .getHandler(request); - } - private HandlerAdapter mapHandlerAdapter(final Object handler) { if (handler instanceof Controller) { return new ControllerHandlerAdapter(handler); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/AnnotationHandlerMapping.java index 532d8d4f3a..27c8f62a95 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/AnnotationHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/AnnotationHandlerMapping.java @@ -32,7 +32,6 @@ public void initialize() { final Set> controllerClasses = reflections.getTypesAnnotatedWith(Controller.class); controllerClasses.forEach(this::addMethodsOnHandler); - System.out.println(handler); } private void addMethodsOnHandler(final Class controllerClass) { @@ -48,8 +47,6 @@ private void addMethodOnHandler(final Class controllerClass, final Method met final String path = controllerClass.getAnnotation(Controller.class).path() + requestMapping.value(); final HandlerExecution handlerExecution = new HandlerExecution(controllerClass, method); - System.out.println("===" + path); - Arrays.stream(requestMapping.method()) .map(requestMethod -> new HandlerKey(path, requestMethod)) .forEach(handlerKey -> handler.put(handlerKey, handlerExecution)); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerMappings.java new file mode 100644 index 0000000000..b7e8941bbe --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerMappings.java @@ -0,0 +1,31 @@ +package webmvc.org.springframework.web.servlet.mvc.handler; + +import jakarta.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.List; +import web.org.springframework.http.exception.MethodNotAllowedException; + +public class HandlerMappings { + + private final List handlerMappings; + + public HandlerMappings() { + this.handlerMappings = new ArrayList<>(); + } + + public void addHandlerMapping(final HandlerMapping handlerMapping) { + handlerMappings.add(handlerMapping); + } + + public void init() { + handlerMappings.forEach(HandlerMapping::initialize); + } + + public Object mapHandler(final HttpServletRequest request) { + return handlerMappings.stream() + .filter(handlerMapping -> handlerMapping.getHandler(request) != null) + .findFirst() + .orElseThrow(() -> new MethodNotAllowedException(request.getMethod())) + .getHandler(request); + } +} From e9f6d619f6503fdf8aa5f9b31127d45343eba083 Mon Sep 17 00:00:00 2001 From: LJW25 Date: Sun, 24 Sep 2023 11:45:38 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat(JspView):=20JspView=20render=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/springframework/web/servlet/view/JspView.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 fb22142428..d77e987e0f 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 @@ -20,14 +20,18 @@ public JspView(final String viewName) { @Override public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - // todo + if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { + response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); + return; + } model.keySet().forEach(key -> { log.debug("attribute name : {}, value : {}", key, model.get(key)); request.setAttribute(key, model.get(key)); }); - // todo + final var requestDispatcher = request.getRequestDispatcher(viewName); + requestDispatcher.forward(request, response); } public String getName() { From c9ed1f572cf4042e9fdda3b3de78466e07246b05 Mon Sep 17 00:00:00 2001 From: LJW25 Date: Sun, 24 Sep 2023 11:46:45 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat(Controller):=20Controller=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EB=B0=98=ED=99=98=20=EB=B0=A9=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/servlet/ControllerHandlerAdapter.java | 3 ++- .../springframework/web/servlet/mvc/asis/Controller.java | 3 ++- .../web/servlet/mvc/asis/ForwardController.java | 7 ++++--- .../web/servlet/mvc/handler/HandlerAdapter.java | 3 ++- .../mvc/handler/HandlerExecutionHandlerAdapter.java | 5 ++--- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.java b/app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.java index 21f7a24585..9ca4678ed6 100644 --- a/app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.java +++ b/app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.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.asis.Controller; import webmvc.org.springframework.web.servlet.mvc.handler.HandlerAdapter; @@ -14,7 +15,7 @@ public ControllerHandlerAdapter(final Object handler) { } @Override - public String execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception { + public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception { return controller.execute(request, response); } } 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 index bdd1fde780..7df4561054 100644 --- 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 @@ -2,7 +2,8 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; public interface Controller { - String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception; + ModelAndView 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 index cd8f1ef371..78963fa324 100644 --- 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 @@ -2,8 +2,9 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; - import java.util.Objects; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; public class ForwardController implements Controller { @@ -14,7 +15,7 @@ public ForwardController(final String path) { } @Override - public String execute(final HttpServletRequest request, final HttpServletResponse response) { - return path; + public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) { + return new ModelAndView(new JspView(path)); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerAdapter.java index 3b149e19c4..420df9066e 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerAdapter.java @@ -2,8 +2,9 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; public interface HandlerAdapter { - Object execute(HttpServletRequest request, HttpServletResponse response) throws Exception; + ModelAndView execute(HttpServletRequest request, HttpServletResponse response) throws Exception; } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerExecutionHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerExecutionHandlerAdapter.java index 22f45e7703..7deb41389c 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerExecutionHandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/handler/HandlerExecutionHandlerAdapter.java @@ -13,8 +13,7 @@ public HandlerExecutionHandlerAdapter(final Object handler) { } @Override - public String execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception { - final ModelAndView modelAndView = handlerExecution.handle(request, response); - return modelAndView.getView().getName(); + public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception { + return handlerExecution.handle(request, response); } } From 9ffd918b24981782bc536010f17022d4a63dc269 Mon Sep 17 00:00:00 2001 From: LJW25 Date: Sun, 24 Sep 2023 11:49:47 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat(*Controller):=20Legacy=20Controller=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LoginController.java | 45 +++++++++++++++---- .../controller/LoginViewController.java | 22 --------- .../controller/LogoutController.java | 20 ++++++--- .../servlet/ManualHandlerMapping.java | 8 +--- 4 files changed, 53 insertions(+), 42 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/controller/LoginViewController.java diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 0428fe109e..4c66902dda 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,20 +2,49 @@ 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(path = "/login") +public class LoginController { + public static final String REDIRECT_INDEX = "redirect:/index.jsp"; + public static final String REDIRECT_401 = "redirect:/401.jsp"; private static final Logger log = LoggerFactory.getLogger(LoginController.class); - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(method = RequestMethod.POST) + public ModelAndView doLogin(final HttpServletRequest req, final HttpServletResponse res) { + final String view = findViewName(req, res); + final var modelAndView = new ModelAndView(new JspView(view)); + modelAndView.addObject("id", req.getAttribute("id")); + + return modelAndView; + } + + @RequestMapping(value = "/view", method = RequestMethod.GET) + public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) { + final String loginView = UserSession.getUserFrom(req.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return REDIRECT_INDEX; + }) + .orElse("/login.jsp"); + final var modelAndView = new ModelAndView(new JspView(loginView)); + modelAndView.addObject("id", req.getAttribute("id")); + + return modelAndView; + } + + public String findViewName(final HttpServletRequest req, final HttpServletResponse res) { if (UserSession.isLoggedIn(req.getSession())) { - return "redirect:/index.jsp"; + return REDIRECT_INDEX; } return InMemoryUserRepository.findByAccount(req.getParameter("account")) @@ -23,15 +52,15 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re log.info("User : {}", user); return login(req, user); }) - .orElse("redirect:/401.jsp"); + .orElse(REDIRECT_401); } 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_INDEX; } - return "redirect:/401.jsp"; + return REDIRECT_401; } } 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..8f27594e7a 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,15 +1,25 @@ 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(path = "/logout") +public class LogoutController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(method = RequestMethod.POST) + public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) { final var session = req.getSession(); session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; + + final String view = "redirect:/"; + final var modelAndView = new ModelAndView(new JspView(view)); + modelAndView.addObject("id", req.getAttribute("id")); + + return modelAndView; } } diff --git a/app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java index 481f78acb3..c8b403fd5a 100644 --- a/app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java @@ -1,8 +1,5 @@ package com.techcourse.servlet; -import com.techcourse.controller.LoginController; -import com.techcourse.controller.LoginViewController; -import com.techcourse.controller.LogoutController; import jakarta.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @@ -21,9 +18,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() @@ -33,7 +27,7 @@ public void initialize() { @Override public Controller getHandler(final HttpServletRequest request) { final String requestURI = request.getRequestURI(); - + log.debug("Request Mapping Uri : {}", requestURI); return controllers.get(requestURI); } From 298ea80af25eaf5592631a9dd9bcf6a50ce0a6bd Mon Sep 17 00:00:00 2001 From: LJW25 Date: Sun, 24 Sep 2023 21:45:43 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat(JsonView):=20JsonView=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LoginController.java | 4 +-- .../techcourse/controller/UserController.java | 33 +++++++++++++++++++ .../http/exception/PageNotFoundException.java | 9 +++++ .../web/servlet/ModelAndView.java | 6 ++++ .../web/servlet/view/JsonView.java | 21 ++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/techcourse/controller/UserController.java create mode 100644 mvc/src/main/java/web/org/springframework/http/exception/PageNotFoundException.java diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 4c66902dda..92d06d86b1 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -15,8 +15,8 @@ @Controller(path = "/login") public class LoginController { - public static final String REDIRECT_INDEX = "redirect:/index.jsp"; - public static final String REDIRECT_401 = "redirect:/401.jsp"; + private static final String REDIRECT_INDEX = "redirect:/index.jsp"; + private static final String REDIRECT_401 = "redirect:/401.jsp"; private static final Logger log = LoggerFactory.getLogger(LoginController.class); @RequestMapping(method = RequestMethod.POST) 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..0d2dafede8 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -0,0 +1,33 @@ +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.http.exception.PageNotFoundException; +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(final HttpServletRequest request, final 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(() -> new PageNotFoundException()); + + modelAndView.addObject("user", user); + return modelAndView; + } +} diff --git a/mvc/src/main/java/web/org/springframework/http/exception/PageNotFoundException.java b/mvc/src/main/java/web/org/springframework/http/exception/PageNotFoundException.java new file mode 100644 index 0000000000..6162daef62 --- /dev/null +++ b/mvc/src/main/java/web/org/springframework/http/exception/PageNotFoundException.java @@ -0,0 +1,9 @@ +package web.org.springframework.http.exception; + +public class PageNotFoundException extends HttpException { + + public PageNotFoundException() { + this.statusCode = 404; + this.message = "요청하신 페이지를 찾을 수 없습니다 :"; + } +} 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..546a045cfe 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; @@ -19,6 +21,10 @@ public ModelAndView addObject(final String attributeName, final Object attribute return this; } + public void render(final HttpServletRequest request, final HttpServletResponse response) throws Exception { + view.render(model, request, response); + } + public Object getObject(final String attributeName) { return model.get(attributeName); } 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 ef4edd93eb..d6c93342f6 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,8 +1,11 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.Map; +import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; public class JsonView implements View { @@ -10,6 +13,24 @@ public class JsonView implements View { @Override public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + + if (model.size() > 0) { + final ServletOutputStream outputStream = response.getOutputStream(); + + final String responseBody = getResponseBody(model); + final byte[] bytes = responseBody.getBytes(); + outputStream.write(bytes); + response.setContentLength(bytes.length); + } + } + + private String getResponseBody(final Map model) throws Exception { + if (model.size() == 1) { + return model.values().toArray()[0].toString(); + } + final ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.writeValueAsString(model); } @Override From 909e317f55bf980cd859b7b5e1a990f2e2bf9208 Mon Sep 17 00:00:00 2001 From: LJW25 Date: Sun, 24 Sep 2023 21:57:36 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat(DispatcherServlet):=20Legacy=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EB=B0=8F=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/TomcatStarter.java | 2 +- .../techcourse/controller/HomeController.java | 18 ++++++++++ .../controller/LoginController.java | 4 +-- .../controller/RegisterController.java | 4 +-- .../techcourse/controller/UserController.java | 2 +- .../servlet/ControllerHandlerAdapter.java | 21 ------------ .../servlet/DispatcherServletInitializer.java | 1 + .../servlet/ManualHandlerMapping.java | 34 ------------------- .../web/filter/CharacterEncodingFilter.java | 10 ++++-- .../support/web/filter/ResourceFilter.java | 13 ++++--- .../web/servlet/mvc/asis/Controller.java | 9 ----- .../servlet/mvc/asis/ForwardController.java | 21 ------------ .../mvc/asis}/servlet/DispatcherServlet.java | 29 ++++------------ .../servlet/UncheckedServletException.java | 2 +- 14 files changed, 48 insertions(+), 122 deletions(-) create mode 100644 app/src/main/java/com/techcourse/controller/HomeController.java delete mode 100644 app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.java delete mode 100644 app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java 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 {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis}/servlet/DispatcherServlet.java (65%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis}/servlet/UncheckedServletException.java (70%) diff --git a/app/src/main/java/com/techcourse/TomcatStarter.java b/app/src/main/java/com/techcourse/TomcatStarter.java index 73449116e1..efaedb7fa6 100644 --- a/app/src/main/java/com/techcourse/TomcatStarter.java +++ b/app/src/main/java/com/techcourse/TomcatStarter.java @@ -1,6 +1,5 @@ package com.techcourse; -import com.techcourse.servlet.UncheckedServletException; import java.io.File; import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; @@ -8,6 +7,7 @@ import org.apache.catalina.core.StandardContext; import org.apache.catalina.startup.Tomcat; import org.apache.tomcat.util.scan.StandardJarScanner; +import webmvc.org.springframework.web.servlet.mvc.asis.servlet.UncheckedServletException; public class TomcatStarter { 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..a1a561b314 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/HomeController.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 HomeController { + + @RequestMapping(value = "/view", method = RequestMethod.GET) + public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) { + 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 index 92d06d86b1..5e61b591e3 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -21,7 +21,7 @@ public class LoginController { @RequestMapping(method = RequestMethod.POST) public ModelAndView doLogin(final HttpServletRequest req, final HttpServletResponse res) { - final String view = findViewName(req, res); + final String view = findViewName(req); final var modelAndView = new ModelAndView(new JspView(view)); modelAndView.addObject("id", req.getAttribute("id")); @@ -42,7 +42,7 @@ public ModelAndView show(final HttpServletRequest req, final HttpServletResponse return modelAndView; } - public String findViewName(final HttpServletRequest req, final HttpServletResponse res) { + public String findViewName(final HttpServletRequest req) { if (UserSession.isLoggedIn(req.getSession())) { return REDIRECT_INDEX; } diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 3a11741a38..a8af90e037 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -15,7 +15,7 @@ public class RegisterController { @RequestMapping(method = RequestMethod.POST) public ModelAndView save(final HttpServletRequest req, final HttpServletResponse res) { - final String view = saveUser(req, res); + final String view = saveUser(req); final var modelAndView = new ModelAndView(new JspView(view)); modelAndView.addObject("id", req.getAttribute("id")); @@ -30,7 +30,7 @@ public ModelAndView show(final HttpServletRequest req, final HttpServletResponse return modelAndView; } - public String saveUser(final HttpServletRequest req, final HttpServletResponse res) { + public String saveUser(final HttpServletRequest req) { final var user = new User(2, req.getParameter("account"), req.getParameter("password"), diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java index 0d2dafede8..e0d8caab5b 100644 --- a/app/src/main/java/com/techcourse/controller/UserController.java +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -25,7 +25,7 @@ public ModelAndView show(final HttpServletRequest request, final HttpServletResp final ModelAndView modelAndView = new ModelAndView(new JsonView()); final User user = InMemoryUserRepository.findByAccount(account) - .orElseThrow(() -> new PageNotFoundException()); + .orElseThrow(PageNotFoundException::new); modelAndView.addObject("user", user); return modelAndView; diff --git a/app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.java b/app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.java deleted file mode 100644 index 9ca4678ed6..0000000000 --- a/app/src/main/java/com/techcourse/servlet/ControllerHandlerAdapter.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.techcourse.servlet; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.mvc.handler.HandlerAdapter; - -public class ControllerHandlerAdapter implements HandlerAdapter { - - private final Controller controller; - - public ControllerHandlerAdapter(final Object handler) { - this.controller = (Controller) handler; - } - - @Override - public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) throws Exception { - return controller.execute(request, response); - } -} diff --git a/app/src/main/java/com/techcourse/servlet/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/servlet/DispatcherServletInitializer.java index 713456a471..f12be223c9 100644 --- a/app/src/main/java/com/techcourse/servlet/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/servlet/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.asis.servlet.DispatcherServlet; /** * Base class for {@link WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in the diff --git a/app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java deleted file mode 100644 index c8b403fd5a..0000000000 --- a/app/src/main/java/com/techcourse/servlet/ManualHandlerMapping.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.techcourse.servlet; - -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.asis.ForwardController; -import webmvc.org.springframework.web.servlet.mvc.handler.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() { - controllers.put("/", new ForwardController("/index.jsp")); - - 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) { - final String requestURI = request.getRequestURI(); - - log.debug("Request Mapping Uri : {}", requestURI); - return controllers.get(requestURI); - } -} diff --git a/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java b/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java index 290fc83b7d..4dc7a307f1 100644 --- a/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java +++ b/app/src/main/java/com/techcourse/support/web/filter/CharacterEncodingFilter.java @@ -1,8 +1,12 @@ package com.techcourse.support.web.filter; -import jakarta.servlet.*; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import jakarta.servlet.annotation.WebFilter; - import java.io.IOException; @WebFilter("/*") @@ -15,7 +19,7 @@ public void init(final FilterConfig filterConfig) throws ServletException { } @Override - public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain) + public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(DEFAULT_ENCODING); response.setCharacterEncoding(DEFAULT_ENCODING); diff --git a/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java b/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java index ddbddd671c..1c5aa8d126 100644 --- a/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java +++ b/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java @@ -1,15 +1,20 @@ package com.techcourse.support.web.filter; -import jakarta.servlet.*; +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; import jakarta.servlet.annotation.WebFilter; import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @WebFilter("/*") public class ResourceFilter implements Filter { 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 7df4561054..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java +++ /dev/null @@ -1,9 +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; - -public interface Controller { - ModelAndView 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 78963fa324..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java +++ /dev/null @@ -1,21 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.util.Objects; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; - -public class ForwardController implements Controller { - - private final String path; - - public ForwardController(final String path) { - this.path = Objects.requireNonNull(path); - } - - @Override - public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) { - return new ModelAndView(new JspView(path)); - } -} diff --git a/app/src/main/java/com/techcourse/servlet/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/servlet/DispatcherServlet.java similarity index 65% rename from app/src/main/java/com/techcourse/servlet/DispatcherServlet.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/servlet/DispatcherServlet.java index 5fb15658a2..f42f218bf7 100644 --- a/app/src/main/java/com/techcourse/servlet/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/servlet/DispatcherServlet.java @@ -1,4 +1,4 @@ -package com.techcourse.servlet; +package webmvc.org.springframework.web.servlet.mvc.asis.servlet; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; @@ -7,27 +7,26 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.http.exception.MethodNotAllowedException; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; +import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.mvc.handler.AnnotationHandlerMapping; import webmvc.org.springframework.web.servlet.mvc.handler.HandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.handler.HandlerExecution; import webmvc.org.springframework.web.servlet.mvc.handler.HandlerExecutionHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.handler.HandlerMappings; -import webmvc.org.springframework.web.servlet.view.JspView; 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(); + private final HandlerMappings handlerMappings; public DispatcherServlet() { + handlerMappings = new HandlerMappings(); } @Override public void init() { - handlerMappings.addHandlerMapping(new ManualHandlerMapping()); handlerMappings.addHandlerMapping(new AnnotationHandlerMapping("com.techcourse")); handlerMappings.init(); } @@ -40,10 +39,8 @@ protected void service(final HttpServletRequest request, final HttpServletRespon try { final Object handler = handlerMappings.mapHandler(request); final HandlerAdapter handlerAdapter = mapHandlerAdapter(handler); - final Object view = handlerAdapter.execute(request, response); - - move((String) view, request, response); - + final ModelAndView modelAndView = handlerAdapter.execute(request, response); + modelAndView.render(request, response); } catch (final Throwable e) { log.error("Exception : {}", e.getMessage(), e); throw new ServletException(e.getMessage()); @@ -51,23 +48,9 @@ protected void service(final HttpServletRequest request, final HttpServletRespon } private HandlerAdapter mapHandlerAdapter(final Object handler) { - if (handler instanceof Controller) { - return new ControllerHandlerAdapter(handler); - } if (handler instanceof HandlerExecution) { return new HandlerExecutionHandlerAdapter(handler); } throw new MethodNotAllowedException(); } - - private void move(final String viewName, final HttpServletRequest request, final HttpServletResponse response) - throws Exception { - 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); - } } diff --git a/app/src/main/java/com/techcourse/servlet/UncheckedServletException.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/servlet/UncheckedServletException.java similarity index 70% rename from app/src/main/java/com/techcourse/servlet/UncheckedServletException.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/servlet/UncheckedServletException.java index 57bfa89d39..93bacfb8b6 100644 --- a/app/src/main/java/com/techcourse/servlet/UncheckedServletException.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/servlet/UncheckedServletException.java @@ -1,4 +1,4 @@ -package com.techcourse.servlet; +package webmvc.org.springframework.web.servlet.mvc.asis.servlet; public class UncheckedServletException extends RuntimeException { From 527985731d4d7e1dfbdc213af82e07a60a6c1658 Mon Sep 17 00:00:00 2001 From: LJW25 Date: Sun, 24 Sep 2023 22:05:11 +0900 Subject: [PATCH 7/8] =?UTF-8?q?feat(all):=20=EC=82=AC=EC=9A=A9=EB=90=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20?= =?UTF-8?q?=EB=B0=8F=20=EB=B3=80=EC=88=98=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/controller/HomeController.java | 2 +- .../java/com/techcourse/controller/LoginController.java | 2 +- .../java/com/techcourse/controller/LogoutController.java | 2 +- .../com/techcourse/controller/RegisterController.java | 3 +++ .../java/com/techcourse/controller/UserController.java | 8 ++++---- .../org/springframework/web/servlet/ModelAndView.java | 4 ---- .../java/webmvc/org/springframework/web/servlet/View.java | 2 -- .../org/springframework/web/servlet/view/JsonView.java | 5 ----- .../org/springframework/web/servlet/view/JspView.java | 4 ---- 9 files changed, 10 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/techcourse/controller/HomeController.java b/app/src/main/java/com/techcourse/controller/HomeController.java index a1a561b314..9bcce7d4fc 100644 --- a/app/src/main/java/com/techcourse/controller/HomeController.java +++ b/app/src/main/java/com/techcourse/controller/HomeController.java @@ -11,7 +11,7 @@ @Controller public class HomeController { - @RequestMapping(value = "/view", method = RequestMethod.GET) + @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) { 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 index 5e61b591e3..20d2785f7b 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -15,9 +15,9 @@ @Controller(path = "/login") public class LoginController { + private static final Logger log = LoggerFactory.getLogger(LoginController.class); private static final String REDIRECT_INDEX = "redirect:/index.jsp"; private static final String REDIRECT_401 = "redirect:/401.jsp"; - private static final Logger log = LoggerFactory.getLogger(LoginController.class); @RequestMapping(method = RequestMethod.POST) public ModelAndView doLogin(final HttpServletRequest req, final HttpServletResponse res) { diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 8f27594e7a..8018190eea 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -11,7 +11,7 @@ @Controller(path = "/logout") public class LogoutController { - @RequestMapping(method = RequestMethod.POST) + @RequestMapping(method = RequestMethod.GET) public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) { final var session = req.getSession(); session.removeAttribute(UserSession.SESSION_KEY); diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index a8af90e037..dd42a9a795 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -37,6 +37,9 @@ public String saveUser(final HttpServletRequest req) { req.getParameter("email")); InMemoryUserRepository.save(user); + final var session = req.getSession(); + session.setAttribute(UserSession.SESSION_KEY, user); + return "redirect:/index.jsp"; } } diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java index e0d8caab5b..9e2edfb104 100644 --- a/app/src/main/java/com/techcourse/controller/UserController.java +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -13,14 +13,14 @@ import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.view.JsonView; -@Controller +@Controller(path = "/api") public class UserController { private static final Logger log = LoggerFactory.getLogger(UserController.class); - @RequestMapping(value = "/api/user", method = RequestMethod.GET) - public ModelAndView show(final HttpServletRequest request, final HttpServletResponse response) { - final String account = request.getParameter("account"); + @RequestMapping(value = "/user", method = RequestMethod.GET) + public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) { + final String account = req.getParameter("account"); log.debug("user id : {}", account); final ModelAndView modelAndView = new ModelAndView(new JsonView()); 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 546a045cfe..f2ec21289c 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 @@ -32,8 +32,4 @@ public Object getObject(final String attributeName) { public Map getModel() { return Collections.unmodifiableMap(model); } - - public View getView() { - return view; - } } 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 98804d96a2..e3dcec2408 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java @@ -6,6 +6,4 @@ public interface View { void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; - - String getName(); } 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 d6c93342f6..4d9abf3b11 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 @@ -32,9 +32,4 @@ private String getResponseBody(final Map model) throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsString(model); } - - @Override - public String getName() { - return ""; - } } 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 d77e987e0f..8df087b766 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 @@ -33,8 +33,4 @@ public void render(final Map model, final HttpServletRequest request, final var requestDispatcher = request.getRequestDispatcher(viewName); requestDispatcher.forward(request, response); } - - public String getName() { - return this.viewName; - } } From f8bb30828fbe4104e43572a5c1f3f4fad5dd006a Mon Sep 17 00:00:00 2001 From: LJW25 Date: Mon, 25 Sep 2023 19:30:17 +0900 Subject: [PATCH 8/8] =?UTF-8?q?refactor(JsonView):=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?write=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/springframework/web/servlet/view/JsonView.java | 7 +------ 1 file changed, 1 insertion(+), 6 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 4d9abf3b11..0bc988edce 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,7 +1,6 @@ package webmvc.org.springframework.web.servlet.view; import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.Map; @@ -16,12 +15,8 @@ public void render(final Map model, final HttpServletRequest request, response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); if (model.size() > 0) { - final ServletOutputStream outputStream = response.getOutputStream(); - final String responseBody = getResponseBody(model); - final byte[] bytes = responseBody.getBytes(); - outputStream.write(bytes); - response.setContentLength(bytes.length); + response.getWriter().write(responseBody); } }