-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MVC 구현하기 - 2단계] 무민(박무현) 미션 제출합니다. (#440)
* refactor: forEach 제거 * chore: legacy controller 패키지 이동 * feat: HandlerMapping 생성 * refactor: ModelAndView view 타입 수정 * feat: HandlerAdapter 생성 * feat: HandlerExecutor 생성 * test: ControllerAdapter 테스트 * test: HandlerExecutionAdapter 테스트 * test: HandlerExecutor 테스트 * refactor: 객체 분리에 따른 DispatcherServlet 수정 * test: DispatcherServlet 테스트 * feat: MvcRegisterController 생성 * test: MvcRegisterController 테스트 * feat: MvcLoginController 생성 * test: MvcLoginController 테스트 * feat: MvcLogoutController 생성 * test: MvcLogoutController 테스트 * refactor: var 수정 * refactor: 중복 코드 ControllerTest 분리 * feat: MvcForwardController 생성 * test: MvcForwardController 테스트 * refactor: controller 한번만 인스턴스화 하도록 수정 * refactor: 로그 위치 변경 * refactor: Scanner 분리 * refactor: handler가 null일 때 수정 * refactor: 마지막줄 개행 추가 * refactor: handlerMapping 공존
- Loading branch information
1 parent
674e092
commit 05db874
Showing
33 changed files
with
990 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
|
||
public class ControllerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(Object handler) { | ||
return (handler instanceof Controller); | ||
} | ||
|
||
@Override | ||
public String adapt(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
return ((Controller) handler).execute(request, response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
public interface HandlerAdapter { | ||
|
||
boolean supports(Object handler); | ||
|
||
String adapt(HttpServletRequest request, final HttpServletResponse response, Object handler) throws Exception; | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/techcourse/HandlerExecutionAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; | ||
|
||
public class HandlerExecutionAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(Object handler) { | ||
return (handler instanceof HandlerExecution); | ||
} | ||
|
||
@Override | ||
public String adapt(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
ModelAndView handle = ((HandlerExecution) handler).handle(request, response); | ||
return (String) handle.getView(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.util.Set; | ||
|
||
public class HandlerExecutor { | ||
|
||
private static final Set<HandlerAdapter> handlerAdapters = Set.of( | ||
new ControllerAdapter(), | ||
new HandlerExecutionAdapter() | ||
); | ||
|
||
public String execute(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
for (HandlerAdapter handlerAdapter : handlerAdapters) { | ||
if (handlerAdapter.supports(handler)) { | ||
return handlerAdapter.adapt(request, response, handler); | ||
} | ||
} | ||
throw new IllegalArgumentException("Not found handler adapter for handler : " + handler); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HandlerMappings { | ||
|
||
private List<HandlerMapping> handlerMappings = new ArrayList<>(); | ||
|
||
public void init() { | ||
handlerMappings.add(new AnnotationHandlerMapping()); | ||
handlerMappings.add(new ManualHandlerMapping()); | ||
for (HandlerMapping handlerMapping : handlerMappings) { | ||
handlerMapping.initialize(); | ||
} | ||
} | ||
|
||
public Object getHandler(HttpServletRequest request) { | ||
for (HandlerMapping handlerMapping : handlerMappings) { | ||
Object handler = handlerMapping.getHandler(request); | ||
if (handler != null) { | ||
return handler; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...echcourse/controller/LoginController.java → ...se/controller/legacy/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ourse/controller/LoginViewController.java → ...ontroller/legacy/LoginViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...chcourse/controller/LogoutController.java → ...e/controller/legacy/LogoutController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...course/controller/RegisterController.java → ...controller/legacy/RegisterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...se/controller/RegisterViewController.java → ...roller/legacy/RegisterViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/techcourse/controller/mvc/MvcForwardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.techcourse.controller.mvc; | ||
|
||
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; | ||
|
||
@Controller | ||
public class MvcForwardController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(MvcForwardController.class); | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView viewIndex(HttpServletRequest req, HttpServletResponse res) { | ||
return new ModelAndView("/index.jsp"); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/techcourse/controller/mvc/MvcLoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.techcourse.controller.mvc; | ||
|
||
import com.techcourse.controller.UserSession; | ||
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 jakarta.servlet.http.HttpSession; | ||
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; | ||
|
||
@Controller | ||
public class MvcLoginController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(MvcLoginController.class); | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.POST) | ||
public ModelAndView loginUser(HttpServletRequest req, HttpServletResponse res) { | ||
if (UserSession.isLoggedIn(req.getSession())) { | ||
return new ModelAndView("redirect:/index.jsp"); | ||
} | ||
return new ModelAndView( | ||
InMemoryUserRepository.findByAccount(req.getParameter("account")) | ||
.map(user -> { | ||
log.info("User : {}", user); | ||
return login(req, user); | ||
}) | ||
.orElse("redirect:/401.jsp") | ||
); | ||
} | ||
|
||
private String login(final HttpServletRequest request, final User user) { | ||
if (user.checkPassword(request.getParameter("password"))) { | ||
HttpSession session = request.getSession(); | ||
session.setAttribute(UserSession.SESSION_KEY, user); | ||
return "redirect:/index.jsp"; | ||
} | ||
return "redirect:/401.jsp"; | ||
} | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.GET) | ||
public ModelAndView viewLogin(HttpServletRequest req, HttpServletResponse res) { | ||
return new ModelAndView( | ||
UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return "redirect:/index.jsp"; | ||
}) | ||
.orElse("/login.jsp") | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/techcourse/controller/mvc/MvcLogoutController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.techcourse.controller.mvc; | ||
|
||
import com.techcourse.controller.UserSession; | ||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
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; | ||
|
||
@Controller | ||
public class MvcLogoutController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(MvcLogoutController.class); | ||
|
||
@RequestMapping(value = "/logout", method = RequestMethod.POST) | ||
public ModelAndView logoutUser(HttpServletRequest req, HttpServletResponse res) { | ||
HttpSession session = req.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return new ModelAndView("redirect:/"); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/techcourse/controller/mvc/MvcRegisterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.techcourse.controller.mvc; | ||
|
||
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; | ||
|
||
@Controller | ||
public class MvcRegisterController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(MvcRegisterController.class); | ||
|
||
@RequestMapping(value = "/register", method = RequestMethod.POST) | ||
public ModelAndView registerUser(HttpServletRequest req, HttpServletResponse res) { | ||
User user = new User(2, | ||
req.getParameter("account"), | ||
req.getParameter("password"), | ||
req.getParameter("email")); | ||
InMemoryUserRepository.save(user); | ||
|
||
return new ModelAndView("redirect:/index.jsp"); | ||
} | ||
|
||
@RequestMapping(value = "/register", method = RequestMethod.GET) | ||
public ModelAndView viewRegister(HttpServletRequest req, HttpServletResponse res) { | ||
return new ModelAndView("/register.jsp"); | ||
} | ||
} |
Oops, something went wrong.