From bc9dc2a238fbc1a2125bd7dee2e4309978a03c35 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 15:23:52 +0900 Subject: [PATCH 01/12] =?UTF-8?q?refactor:=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EB=A7=A4=ED=95=91=EB=A7=8C=20=EB=82=A8?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/IndexController.java | 18 +++++++++++++ .../controller/LoginController.java | 25 +++++++++++-------- .../controller/LoginViewController.java | 20 +++++++-------- .../controller/LogoutController.java | 17 +++++-------- 4 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/com/techcourse/controller/IndexController.java diff --git a/app/src/main/java/com/techcourse/controller/IndexController.java b/app/src/main/java/com/techcourse/controller/IndexController.java new file mode 100644 index 0000000000..a9ab4fb105 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/IndexController.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 IndexController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView index(HttpServletRequest req, 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 35bac1b026..96908d1af4 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,22 +2,29 @@ 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.mvc.asis.Controller; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; 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) { + + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView login(HttpServletRequest req, HttpServletResponse res) { + String path = execute(req, res); + return new ModelAndView(new JspView(path)); + } + + private String execute(HttpServletRequest req, HttpServletResponse res) { if (UserSession.isLoggedIn(req.getSession())) { return "redirect:/index.jsp"; } @@ -30,7 +37,7 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re .orElse("redirect:/401.jsp"); } - private String login(final HttpServletRequest request, final User user) { + private String login(HttpServletRequest request, User user) { if (user.checkPassword(request.getParameter("password"))) { final var session = request.getSession(); session.setAttribute(UserSession.SESSION_KEY, user); @@ -39,8 +46,4 @@ private String login(final HttpServletRequest request, final User user) { return "redirect:/401.jsp"; } - @RequestMapping(value = "/login", method = RequestMethod.GET) - public ModelAndView login(HttpServletRequest req, HttpServletResponse res) { - return new ModelAndView(new JspView(execute(req, res))); - } } diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java index e581f172de..950d1240c8 100644 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ b/app/src/main/java/com/techcourse/controller/LoginViewController.java @@ -1,5 +1,6 @@ package com.techcourse.controller; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; @@ -7,15 +8,20 @@ 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.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; -public class LoginViewController implements Controller { +@Controller +public class LoginViewController { private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) { + @RequestMapping(value = "/login/view", method = RequestMethod.GET) + public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { + String path = execute(req, res); + return new ModelAndView(new JspView(path)); + } + + private String execute(HttpServletRequest req, HttpServletResponse res) { return UserSession.getUserFrom(req.getSession()) .map(user -> { log.info("logged in {}", user.getAccount()); @@ -23,10 +29,4 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re }) .orElse("/login.jsp"); } - - @RequestMapping(value = "/login/view", method = RequestMethod.GET) - public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); - return new ModelAndView(new JspView(path)); - } } diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 777eb25afb..d89bf4af19 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,25 +1,20 @@ 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.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; -public class LogoutController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) { - final var session = req.getSession(); - session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; - } +@Controller +public class LogoutController { @RequestMapping(value = "/logout", method = RequestMethod.POST) public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); - return new ModelAndView(new JspView(path)); + final var session = req.getSession(); + session.removeAttribute(UserSession.SESSION_KEY); + return new ModelAndView(new JspView("redirect:/")); } } From 392375176c9481bca905522b6ca914b7605e7fd6 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 15:24:12 +0900 Subject: [PATCH 02/12] =?UTF-8?q?feat:=20ManualHandlerMapping=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/ManualHandlerMapping.java | 40 ------------------- 1 file changed, 40 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/ManualHandlerMapping.java 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 cb8aafe043..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.techcourse; - -import com.techcourse.controller.*; -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 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); - } -} From 4c1edbe34bb505e0de500cb6ccd3c4505f5ab4e7 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 15:25:39 +0900 Subject: [PATCH 03/12] =?UTF-8?q?feat:=20user=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/controller/UserController.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/src/main/java/com/techcourse/controller/UserController.java diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java new file mode 100644 index 0000000000..c1361f487b --- /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; + } +} From 1e23c9f2da492d391b2923176a7863b33a33b6fe Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 15:26:27 +0900 Subject: [PATCH 04/12] =?UTF-8?q?refactor:=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=EB=B9=BC=EB=A1=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/RegisterController.java | 22 +++++++------------ .../controller/RegisterViewController.java | 13 ++++------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 8dffb57bf4..e04b681fba 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -2,31 +2,25 @@ 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.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; -public class RegisterController implements Controller { +@Controller +public class RegisterController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - final var user = new User(2, + @RequestMapping(value = "/register", method = RequestMethod.POST) + public ModelAndView save(HttpServletRequest req, HttpServletResponse res) { + final User user = new User(2, req.getParameter("account"), req.getParameter("password"), req.getParameter("email")); InMemoryUserRepository.save(user); - - return "redirect:/index.jsp"; - } - - @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { - String path = execute(request, response); - return new ModelAndView(new JspView(path)); + JspView view = new JspView("redirect:/index.jsp"); + return new ModelAndView(view); } - } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java index 5daa6d5e9f..ab6543f6c5 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterViewController.java @@ -1,23 +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.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; -public class RegisterViewController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) { - return "/register.jsp"; - } +@Controller +public class RegisterViewController { @RequestMapping(value = "/register/view", method = RequestMethod.GET) public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); - return new ModelAndView(new JspView(path)); + return new ModelAndView(new JspView("/register.jsp")); } } From a421497bb16f8b2fd76f78f0235e5dbd3050071e Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 15:26:56 +0900 Subject: [PATCH 05/12] =?UTF-8?q?feat:=20jsonView=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/servlet/view/JsonView.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) 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..9bafe15f57 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,45 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; +import java.io.PrintWriter; import java.util.Map; +import java.util.NoSuchElementException; public class JsonView implements View { + private static final ObjectMapper objectMapper = new ObjectMapper(); + @Override public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + final String json = getJsonString(model); + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + final PrintWriter writer = response.getWriter(); + writer.write(json); + writer.flush(); + } + + private String getJsonString(Map model) { + if (model.size() == 1) { + return model.values().stream() + .map(this::writeValueAsString) + .findAny() + .orElseThrow(NoSuchElementException::new); + } + return writeValueAsString(model); } + + private String writeValueAsString(Object object) { + try { + return objectMapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException("JsonProcessingException: ", e); + } + } + } From 7b2cbbc191a4a9d044aa290bfba7dc94f5798985 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 15:27:27 +0900 Subject: [PATCH 06/12] =?UTF-8?q?refactor:=20=EA=B8=B0=EC=A1=B4=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=ED=99=94=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/DispatcherServletInitializer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index a5ca691f2d..4183cc06cf 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -21,9 +21,9 @@ public class DispatcherServletInitializer implements WebApplicationInitializer { @Override public void onStartup(final ServletContext servletContext) { final DispatcherServlet dispatcherServlet = new DispatcherServlet(); - dispatcherServlet.addHandlerMapping(new ManualHandlerMapping()); + // dispatcherServlet.addHandlerMapping(new ManualHandlerMapping()); dispatcherServlet.addHandlerMapping(new AnnotationHandlerMapping("com")); - dispatcherServlet.addHandlerAdapter(new ControllerHandlerAdapter()); + // dispatcherServlet.addHandlerAdapter(new ControllerHandlerAdapter()); dispatcherServlet.addHandlerAdapter(new HandlerExecutionHandlerAdapter()); final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet); From 82b6dd7f59b7a144f66df4b34032e6cb69d24c18 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 15:34:08 +0900 Subject: [PATCH 07/12] =?UTF-8?q?style:=20final=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20=EA=B0=9C=ED=96=89=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/DispatcherServlet.java | 2 +- .../com/techcourse/DispatcherServletInitializer.java | 3 --- .../com/techcourse/controller/IndexController.java | 3 ++- .../com/techcourse/controller/LoginController.java | 11 +++++++---- .../techcourse/controller/LoginViewController.java | 8 +++++--- .../com/techcourse/controller/LogoutController.java | 3 ++- .../com/techcourse/controller/RegisterController.java | 5 +++-- .../techcourse/controller/RegisterViewController.java | 3 ++- .../com/techcourse/controller/UserController.java | 3 ++- .../org/springframework/web/servlet/ModelAndView.java | 3 ++- .../web/servlet/mvc/asis/ForwardController.java | 3 ++- .../servlet/mvc/tobe/AnnotationHandlerMapping.java | 5 +++-- .../springframework/web/servlet/view/JsonView.java | 4 +++- .../org/springframework/web/servlet/view/JspView.java | 6 ++++-- 14 files changed, 38 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 6d296881e0..20c47304f7 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -42,7 +42,7 @@ protected void service(final HttpServletRequest request, final HttpServletRespon } private void render(ModelAndView modelAndView, HttpServletRequest req, HttpServletResponse res) throws Exception { - View view = modelAndView.getView(); + final View view = modelAndView.getView(); view.render(modelAndView.getModel(), req, res); } diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 4183cc06cf..610f865c6f 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -4,7 +4,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.web.WebApplicationInitializer; -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; @@ -21,9 +20,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer { @Override public void onStartup(final ServletContext servletContext) { final DispatcherServlet dispatcherServlet = new DispatcherServlet(); - // dispatcherServlet.addHandlerMapping(new ManualHandlerMapping()); dispatcherServlet.addHandlerMapping(new AnnotationHandlerMapping("com")); - // dispatcherServlet.addHandlerAdapter(new ControllerHandlerAdapter()); dispatcherServlet.addHandlerAdapter(new HandlerExecutionHandlerAdapter()); final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet); diff --git a/app/src/main/java/com/techcourse/controller/IndexController.java b/app/src/main/java/com/techcourse/controller/IndexController.java index a9ab4fb105..a84d7ce2f6 100644 --- a/app/src/main/java/com/techcourse/controller/IndexController.java +++ b/app/src/main/java/com/techcourse/controller/IndexController.java @@ -12,7 +12,8 @@ public class IndexController { @RequestMapping(value = "/", method = RequestMethod.GET) - public ModelAndView index(HttpServletRequest req, HttpServletResponse res) { + public ModelAndView index(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 96908d1af4..44b7507d6d 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -19,12 +19,14 @@ public class LoginController { @RequestMapping(value = "/login", method = RequestMethod.POST) - public ModelAndView login(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); + public ModelAndView login(final HttpServletRequest req, + final HttpServletResponse res) { + final String path = execute(req, res); return new ModelAndView(new JspView(path)); } - private String execute(HttpServletRequest req, HttpServletResponse res) { + private String execute(final HttpServletRequest req, + final HttpServletResponse res) { if (UserSession.isLoggedIn(req.getSession())) { return "redirect:/index.jsp"; } @@ -37,7 +39,8 @@ private String execute(HttpServletRequest req, HttpServletResponse res) { .orElse("redirect:/401.jsp"); } - private String login(HttpServletRequest request, User user) { + 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); diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java index 950d1240c8..af07cbbb00 100644 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ b/app/src/main/java/com/techcourse/controller/LoginViewController.java @@ -16,12 +16,14 @@ public class LoginViewController { private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); @RequestMapping(value = "/login/view", method = RequestMethod.GET) - public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); + public ModelAndView loginView(final HttpServletRequest req, + final HttpServletResponse res) { + final String path = execute(req, res); return new ModelAndView(new JspView(path)); } - private String execute(HttpServletRequest req, HttpServletResponse res) { + private String execute(final HttpServletRequest req, + final HttpServletResponse res) { return UserSession.getUserFrom(req.getSession()) .map(user -> { log.info("logged in {}", user.getAccount()); diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index d89bf4af19..122cb57595 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -12,7 +12,8 @@ public class LogoutController { @RequestMapping(value = "/logout", method = RequestMethod.POST) - public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) { + public ModelAndView logout(final HttpServletRequest req, + final HttpServletResponse res) { final var session = req.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 index e04b681fba..3b2b074494 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -14,13 +14,14 @@ public class RegisterController { @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView save(HttpServletRequest req, HttpServletResponse res) { + public ModelAndView save(final HttpServletRequest req, + final HttpServletResponse res) { final User user = new User(2, req.getParameter("account"), req.getParameter("password"), req.getParameter("email")); InMemoryUserRepository.save(user); - JspView view = new JspView("redirect:/index.jsp"); + final JspView view = new JspView("redirect:/index.jsp"); return new ModelAndView(view); } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java index ab6543f6c5..aedb60ff41 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterViewController.java @@ -12,7 +12,8 @@ public class RegisterViewController { @RequestMapping(value = "/register/view", method = RequestMethod.GET) - public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { + public ModelAndView loginView(final HttpServletRequest req, + final HttpServletResponse res) { 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 index c1361f487b..cd6cf39689 100644 --- a/app/src/main/java/com/techcourse/controller/UserController.java +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -18,7 +18,8 @@ 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) { + public ModelAndView show(final HttpServletRequest request, + final HttpServletResponse response) { final String account = request.getParameter("account"); log.debug("user id : {}", account); 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..a38f24b195 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 @@ -14,7 +14,8 @@ public ModelAndView(final View view) { this.model = new HashMap<>(); } - public ModelAndView addObject(final String attributeName, final Object attributeValue) { + public ModelAndView addObject(final String attributeName, + final Object attributeValue) { model.put(attributeName, attributeValue); return this; } 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..2cf67b5839 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 @@ -14,7 +14,8 @@ public ForwardController(final String path) { } @Override - public String execute(final HttpServletRequest request, final HttpServletResponse response) { + 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 c300a83a75..ba6dcf1cc3 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 @@ -40,7 +40,8 @@ public void initialize() { } } - private void createHandlerExecution(final Method[] methods, final Object controllerInstance) { + private void createHandlerExecution(final Method[] methods, + final Object controllerInstance) { for (Method method : methods) { if (method.isAnnotationPresent(RequestMapping.class)) { final RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); @@ -62,7 +63,7 @@ private void addHandlerExecutions(final HandlerExecution handlerExecution, } } - private Object getInstance(Class controller) { + private Object getInstance(final Class controller) { try { return controller.getConstructor().newInstance(); } catch (Exception e) { 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 9bafe15f57..4d4427089f 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 @@ -16,7 +16,9 @@ public class JsonView implements View { private static final ObjectMapper objectMapper = new ObjectMapper(); @Override - public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + public void render(final Map model, + final HttpServletRequest request, + final HttpServletResponse response) throws Exception { final String json = getJsonString(model); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); final PrintWriter writer = response.getWriter(); 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 1275a38089..6ac9de5ba7 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 @@ -16,12 +16,14 @@ public class JspView implements View { private final String viewName; - public JspView(String viewName) { + public JspView(final String viewName) { this.viewName = viewName; } @Override - public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { + public void render(final Map model, + final HttpServletRequest request, + final HttpServletResponse response) throws Exception { model.keySet().forEach(key -> { log.debug("attribute name : {}, value : {}", key, model.get(key)); request.setAttribute(key, model.get(key)); From 008358b36a9bde764e63dc357e529f4550cd03c6 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 25 Sep 2023 16:04:32 +0900 Subject: [PATCH 08/12] =?UTF-8?q?style:=20=ED=86=B5=EC=9D=BC=EB=90=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EC=9D=80=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/DispatcherServlet.java | 4 ++-- .../java/com/techcourse/HandlerAdapters.java | 1 + .../techcourse/controller/LoginController.java | 16 ++++++++-------- .../controller/LoginViewController.java | 12 ++++++------ .../techcourse/controller/LogoutController.java | 6 +++--- .../controller/RegisterController.java | 10 +++++----- .../support/web/filter/ResourceFilter.java | 4 ++-- 7 files changed, 27 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 20c47304f7..10e0b3b4fb 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -41,9 +41,9 @@ protected void service(final HttpServletRequest request, final HttpServletRespon } } - private void render(ModelAndView modelAndView, HttpServletRequest req, HttpServletResponse res) throws Exception { + private void render(ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response) throws Exception { final View view = modelAndView.getView(); - view.render(modelAndView.getModel(), req, res); + view.render(modelAndView.getModel(), request, response); } public void addHandlerMapping(HandlerMapping handlerMapping) { diff --git a/app/src/main/java/com/techcourse/HandlerAdapters.java b/app/src/main/java/com/techcourse/HandlerAdapters.java index f4bb1bb4f9..1cb824de1f 100644 --- a/app/src/main/java/com/techcourse/HandlerAdapters.java +++ b/app/src/main/java/com/techcourse/HandlerAdapters.java @@ -20,5 +20,6 @@ public HandlerAdapter getHandlerAdapter(Object handler) { .filter(adapter -> adapter.supports(handler)) .findAny() .orElseThrow(() -> new IllegalArgumentException("핸들러 어뎁터를 찾지 못했습니다.: " + handler)); + } } diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 44b7507d6d..880c64923b 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -19,22 +19,22 @@ public class LoginController { @RequestMapping(value = "/login", method = RequestMethod.POST) - public ModelAndView login(final HttpServletRequest req, - final HttpServletResponse res) { - final String path = execute(req, res); + public ModelAndView login(final HttpServletRequest request, + final HttpServletResponse response) { + final String path = execute(request, response); return new ModelAndView(new JspView(path)); } - private String execute(final HttpServletRequest req, - final HttpServletResponse res) { - if (UserSession.isLoggedIn(req.getSession())) { + private String execute(final HttpServletRequest request, + final HttpServletResponse response) { + if (UserSession.isLoggedIn(request.getSession())) { return "redirect:/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"); } diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java index af07cbbb00..c550a608ad 100644 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ b/app/src/main/java/com/techcourse/controller/LoginViewController.java @@ -16,15 +16,15 @@ public class LoginViewController { private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); @RequestMapping(value = "/login/view", method = RequestMethod.GET) - public ModelAndView loginView(final HttpServletRequest req, - final HttpServletResponse res) { - final String path = execute(req, res); + public ModelAndView loginView(final HttpServletRequest request, + final HttpServletResponse response) { + final String path = execute(request, response); return new ModelAndView(new JspView(path)); } - private String execute(final HttpServletRequest req, - final HttpServletResponse res) { - return UserSession.getUserFrom(req.getSession()) + private String execute(final HttpServletRequest request, + final HttpServletResponse response) { + return UserSession.getUserFrom(request.getSession()) .map(user -> { log.info("logged in {}", user.getAccount()); return "redirect:/index.jsp"; diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 122cb57595..26c1320e62 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -12,9 +12,9 @@ public class LogoutController { @RequestMapping(value = "/logout", method = RequestMethod.POST) - public ModelAndView logout(final HttpServletRequest req, - final HttpServletResponse res) { - final var session = req.getSession(); + public ModelAndView logout(final HttpServletRequest request, + final 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 index 3b2b074494..0cad0a933e 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -14,12 +14,12 @@ public class RegisterController { @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView save(final HttpServletRequest req, - final HttpServletResponse res) { + public ModelAndView save(final HttpServletRequest request, + final HttpServletResponse response) { final User 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); final JspView view = new JspView("redirect:/index.jsp"); return new ModelAndView(view); 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..47b3c6097c 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 @@ -39,8 +39,8 @@ public void init(final FilterConfig filterConfig) throws ServletException { @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { - final var req = (HttpServletRequest) request; - final var path = req.getRequestURI().substring(req.getContextPath().length()); + final var httpServletRequest = (HttpServletRequest) request; + final var path = httpServletRequest.getRequestURI().substring(httpServletRequest.getContextPath().length()); if (isResourceUrl(path)) { log.debug("path : {}", path); requestDispatcher.forward(request, response); From ef44a00493527b68228f7687e65b312a8d567b94 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Wed, 11 Oct 2023 01:21:40 +0900 Subject: [PATCH 09/12] =?UTF-8?q?refactor:=20=EB=A0=88=EA=B1=B0=EC=8B=9C?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/servlet/mvc/asis/Controller.java | 8 ------- .../mvc/asis/ControllerHandlerAdapter.java | 24 ------------------- .../servlet/mvc/asis/ForwardController.java | 21 ---------------- 3 files changed, 53 deletions(-) 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/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 cf586549fb..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java +++ /dev/null @@ -1,24 +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 supports(final Object handler) { - return handler instanceof Controller; - } - - @Override - public ModelAndView handle(final HttpServletRequest request, - final HttpServletResponse response, - final Object handler) throws Exception { - final Controller controller = (Controller) handler; - return new ModelAndView(new JspView(controller.execute(request, response))); - } - -} 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 2cf67b5839..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; - -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; - } -} From ee3455fdcb29f3a5501d868ef9f75e4bd2bc22df Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Wed, 11 Oct 2023 01:21:50 +0900 Subject: [PATCH 10/12] =?UTF-8?q?refactor:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/DispatcherServletInitializer.java | 1 + .../java/com/techcourse/controller/LogoutController.java | 5 ++--- .../springframework/web/servlet/mvc}/DispatcherServlet.java | 4 +--- .../springframework/web/servlet/mvc}/HandlerAdapters.java | 6 +----- .../springframework/web/servlet/mvc}/HandlerMappings.java | 5 +---- 5 files changed, 6 insertions(+), 15 deletions(-) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/DispatcherServlet.java (92%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/HandlerAdapters.java (81%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/HandlerMappings.java (86%) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 610f865c6f..4e18625a40 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; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionHandlerAdapter; diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 26c1320e62..f9c39099be 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -11,9 +11,8 @@ @Controller public class LogoutController { - @RequestMapping(value = "/logout", method = RequestMethod.POST) - public ModelAndView logout(final HttpServletRequest request, - final HttpServletResponse response) { + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView logout(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/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java similarity index 92% 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 10e0b3b4fb..568dbf505f 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; @@ -8,8 +8,6 @@ import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.View; -import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; -import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; public class DispatcherServlet extends HttpServlet { 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 1cb824de1f..3f35034ba4 100644 --- a/app/src/main/java/com/techcourse/HandlerAdapters.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapters.java @@ -1,7 +1,4 @@ -package com.techcourse; - -import jakarta.annotation.Nonnull; -import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; +package webmvc.org.springframework.web.servlet.mvc; import java.util.ArrayList; import java.util.List; @@ -14,7 +11,6 @@ public void addHandlerAdapter(HandlerAdapter handlerAdapter) { handlerAdapters.add(handlerAdapter); } - @Nonnull public HandlerAdapter getHandlerAdapter(Object handler) { return handlerAdapters.stream() .filter(adapter -> adapter.supports(handler)) 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 86% 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 32591b4929..8773063799 100644 --- a/app/src/main/java/com/techcourse/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java @@ -1,8 +1,6 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; -import jakarta.annotation.Nonnull; import jakarta.servlet.http.HttpServletRequest; -import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; import java.util.ArrayList; import java.util.List; @@ -16,7 +14,6 @@ public void initializeEach() { handlerMappings.forEach(HandlerMapping::initialize); } - @Nonnull public Object getHandler(HttpServletRequest request) { return handlerMappings.stream() .map(handlerMapping -> handlerMapping.getHandler(request)) From 97fd8e5b7eda8113eece809670fd29934700fe9b Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Wed, 11 Oct 2023 01:22:23 +0900 Subject: [PATCH 11/12] =?UTF-8?q?refactor:=20=EB=B3=80=EC=88=98=EB=AA=85?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webmvc/org/springframework/web/servlet/view/JsonView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 4d4427089f..f1495e2d9e 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 @@ -19,10 +19,10 @@ public class JsonView implements View { public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - final String json = getJsonString(model); + final String jsonBody = getJsonString(model); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); final PrintWriter writer = response.getWriter(); - writer.write(json); + writer.write(jsonBody); writer.flush(); } From 7f80dae473b7ad3fc85e7e9cb230a1176f12da6d Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Wed, 11 Oct 2023 01:23:27 +0900 Subject: [PATCH 12/12] =?UTF-8?q?refactor:=20=EC=BB=A8=EB=B2=A4=EC=85=98?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webmvc/org/springframework/web/servlet/view/JsonView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 f1495e2d9e..479c0848e4 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 @@ -26,7 +26,7 @@ public void render(final Map model, writer.flush(); } - private String getJsonString(Map model) { + private String getJsonString(final Map model) { if (model.size() == 1) { return model.values().stream() .map(this::writeValueAsString) @@ -36,7 +36,7 @@ private String getJsonString(Map model) { return writeValueAsString(model); } - private String writeValueAsString(Object object) { + private String writeValueAsString(final Object object) { try { return objectMapper.writeValueAsString(object); } catch (JsonProcessingException e) {