-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MVC 구현하기 2단계] 마코(이규성) 미션 제출합니다. #458
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f29a04b
refactor: stream for each 제거
aak2075 564552d
feat: HandlerMapping 추가
aak2075 b83e0e6
feat: HandlerAdapter 추가
aak2075 2198a32
refactor: 패키지 이동
aak2075 177260f
refactor: 불필요한 import 제거
aak2075 7046e05
refactor: 변수 정리
aak2075 af60665
refactor: 레거시 MVC HandlerExecution -> Controller 사용하도록 수정
aak2075 ccd7119
refactor: 피드백 반영
aak2075 6706838
refactor: RegisterController 어노테이션 기반으로 변경
aak2075 dabd9d2
test: register 요청에 대한 DispatcherServlet 테스트 추가
aak2075 bfabcac
test: 깨지는 테스트 수정
aak2075 9535457
refactor: instanceof로 핸들러 지원 판단 로직 수정
aak2075 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
15 changes: 10 additions & 5 deletions
15
app/src/main/java/com/techcourse/controller/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
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 RegisterViewController implements Controller { | ||
@Controller | ||
public class RegisterViewController { | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return "/register.jsp"; | ||
@RequestMapping(value = "/register", method = RequestMethod.GET) | ||
public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return new ModelAndView(new JspView("/register.jsp")); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/techcourse/mvc/DispatcherServlet.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,46 @@ | ||
package com.techcourse.mvc; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; | ||
|
||
public class DispatcherServlet extends HttpServlet { | ||
|
||
private static final long serialVersionUID = 1L; | ||
private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); | ||
|
||
private HandlerMapping handlerMapping; | ||
private HandlerAdapter handlerAdapter; | ||
|
||
public DispatcherServlet(final HandlerMapping handlerMapping, final HandlerAdapter handlerAdapter) { | ||
this.handlerMapping = handlerMapping; | ||
this.handlerAdapter = handlerAdapter; | ||
} | ||
|
||
@Override | ||
public void init() { | ||
handlerMapping.initialize(); | ||
} | ||
|
||
@Override | ||
protected void service(final HttpServletRequest request, final HttpServletResponse response) | ||
throws ServletException { | ||
final String requestURI = request.getRequestURI(); | ||
log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI); | ||
|
||
try { | ||
final Object handler = handlerMapping.getHandler(request); | ||
final ModelAndView modelAndView = handlerAdapter.handle(request, response, handler); | ||
modelAndView.render(request, response); | ||
} catch (Throwable e) { | ||
log.error("Exception : {}", e.getMessage(), e); | ||
throw new ServletException(e.getMessage()); | ||
} | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/techcourse/mvc/ManualHandlerAdapter.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,30 @@ | ||
package com.techcourse.mvc; | ||
|
||
import com.techcourse.mvc.exception.CanNotInvokeMethodException; | ||
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.mvc.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class ManualHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response, | ||
final Object handler) { | ||
try { | ||
final var controller = (Controller) handler; | ||
final var viewName = controller.execute(request, response); | ||
return new ModelAndView(new JspView(viewName)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new CanNotInvokeMethodException(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supports(Object handler) { | ||
return handler instanceof Controller; | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/techcourse/mvc/ManualHandlerMappingAdapter.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,23 @@ | ||
package com.techcourse.mvc; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; | ||
|
||
public class ManualHandlerMappingAdapter implements HandlerMapping { | ||
|
||
private final ManualHandlerMapping manualHandlerMapping; | ||
|
||
public ManualHandlerMappingAdapter() { | ||
manualHandlerMapping = new ManualHandlerMapping(); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
manualHandlerMapping.initialize(); | ||
} | ||
|
||
@Override | ||
public Object getHandler(final HttpServletRequest httpServletRequest) { | ||
return manualHandlerMapping.getHandler(httpServletRequest.getRequestURI()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
app/src/main/java/com/techcourse/mvc/exception/CanNotInvokeMethodException.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,4 @@ | ||
package com.techcourse.mvc.exception; | ||
|
||
public class CanNotInvokeMethodException extends RuntimeException { | ||
} |
2 changes: 1 addition & 1 deletion
2
...techcourse/UncheckedServletException.java → .../exception/UncheckedServletException.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
64 changes: 64 additions & 0 deletions
64
app/src/test/java/com/techcourse/mvc/DispatcherServletTest.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,64 @@ | ||
package com.techcourse.mvc; | ||
|
||
import jakarta.servlet.RequestDispatcher; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.junit.jupiter.api.Test; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
class DispatcherServletTest { | ||
|
||
@Test | ||
void service_RegisterViewController() throws Exception { | ||
// given | ||
final var handlerMapping = new AnnotationHandlerMapping("com.techcourse.controller"); | ||
handlerMapping.initialize(); | ||
final var handlerAdapter = new AnnotationHandlerAdapter(); | ||
final var dispatcherServlet = new DispatcherServlet(handlerMapping, handlerAdapter); | ||
|
||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
final var requestDispatcher = mock(RequestDispatcher.class); | ||
|
||
when(request.getRequestURI()).thenReturn("/register"); | ||
when(request.getMethod()).thenReturn("GET"); | ||
when(request.getRequestDispatcher("/register.jsp")).thenReturn(requestDispatcher); | ||
doNothing().when(requestDispatcher).forward(any(), any()); | ||
|
||
// when | ||
dispatcherServlet.service(request, response); | ||
|
||
// then | ||
verify(request, times(1)).getRequestDispatcher("/register.jsp"); | ||
verify(requestDispatcher, times(1)).forward(request, response); | ||
} | ||
|
||
@Test | ||
void service_RegisterController() throws Exception { | ||
// given | ||
final var handlerMapping = new AnnotationHandlerMapping("com.techcourse.controller"); | ||
handlerMapping.initialize(); | ||
final var handlerAdapter = new AnnotationHandlerAdapter(); | ||
final var dispatcherServlet = new DispatcherServlet(handlerMapping, handlerAdapter); | ||
|
||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
final var requestDispatcher = mock(RequestDispatcher.class); | ||
|
||
when(request.getRequestURI()).thenReturn("/register"); | ||
when(request.getMethod()).thenReturn("POST"); | ||
when(request.getParameter(any())).thenReturn("anyParam"); | ||
when(request.getRequestDispatcher("/index.jsp")).thenReturn(requestDispatcher); | ||
doNothing().when(requestDispatcher).forward(any(), any()); | ||
|
||
// when | ||
dispatcherServlet.service(request, response); | ||
|
||
// then | ||
verify(request, times(1)).getRequestDispatcher("/index.jsp"); | ||
verify(requestDispatcher, times(1)).forward(request, response); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
한번 더 추상화되어야하는 이유가 궁금해요
manualHandlerMapping이 바로 handlerMapping을 구현하는 형태면 생기는 문제가 뭐가 있을까요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
레거시 코드에 약간의 변경도 허용하고 싶지 않았습니다. 구현을 한다는 것도 변경이고 이로 인해 getHandler의 반환 타입이 Controller -> Object로 바뀌어야 하는데요, 작은 변경이기는 하지만 다른 코드에서 사용하고 있다면 문제가 생길 수도 있고 사실 다른 프레임워크를 적용했는데 시그니처도 같고 구현만 하면 호환이 된다는 것 자체가 좀 와닿지가 않았습니다.
정리하자면 구현으로 간단하게 해결할 수 있지만 실제로 프레임워크를 바꾼다는 생각으로 어댑터를 추가했습니다.
(미션에서 요구한건 컨트롤러만 변경하지 말라는 것인데 지금 봤네요)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋네요 이런 생각은 전혀 못햇어요