-
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 구현하기 - 1단계] 유콩(김유빈) 미션 제출합니다. (#169)
* docs: 1단계 체크리스트 작성 * feat: @controller 읽어와 핸들러 추가 * feat: 핸들러 메서드 실행 * feat: DispatcherServlet 초기화 시 어노테이션 기반 핸들러 매핑 추가 * feat: HandlerAdapter 이용하여 Handler 실제 로직 수행 * feat: ModelAndView rendering
- Loading branch information
Showing
15 changed files
with
296 additions
and
22 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 |
---|---|---|
@@ -1 +1,5 @@ | ||
# @MVC 구현하기 | ||
|
||
## 🚀 1단계 - @MVC 프레임워크 구현하기 | ||
- [x] AnnotationHandlerMappingTest가 정상 동작한다. | ||
- [x] DispatcherServlet에서 HandlerMapping 인터페이스를 활용하여 AnnotationHandlerMapping과 ManualHandlerMapping 둘다 처리할 수 있다. |
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/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,21 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.mvc.HandlerAdapter; | ||
import nextstep.mvc.controller.asis.Controller; | ||
import nextstep.mvc.view.ModelAndView; | ||
|
||
public class ManualHandlerAdapter 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 String viewName = ((Controller) handler).execute(request, response); | ||
return new ModelAndView(viewName); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
mvc/src/main/java/nextstep/mvc/controller/tobe/AnnotationHandlerAdapter.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 nextstep.mvc.controller.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.mvc.HandlerAdapter; | ||
import nextstep.mvc.view.ModelAndView; | ||
import nextstep.web.annotation.Controller; | ||
|
||
public class AnnotationHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(final Object handler) { | ||
return handler.getClass().isAnnotationPresent(Controller.class); | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception { | ||
return ((HandlerExecution) handler).handle(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
27 changes: 26 additions & 1 deletion
27
mvc/src/main/java/nextstep/mvc/controller/tobe/HandlerExecution.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,12 +1,37 @@ | ||
package nextstep.mvc.controller.tobe; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.mvc.view.ModelAndView; | ||
|
||
public class HandlerExecution { | ||
|
||
private final static Logger log = LoggerFactory.getLogger(HandlerExecution.class); | ||
|
||
private final Object clazz; | ||
private final Method method; | ||
|
||
public HandlerExecution(final Object clazz, final Method method) { | ||
this.clazz = clazz; | ||
this.method = method; | ||
} | ||
|
||
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
return null; | ||
final ModelAndView modelAndView = (ModelAndView) method.invoke(clazz, request, response); | ||
log.info("execute controller method [{}]", method); | ||
return modelAndView; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "HandlerExecution{" + | ||
"clazz=" + clazz + | ||
", method=" + method + | ||
'}'; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,16 @@ | ||
package nextstep.web.support; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum RequestMethod { | ||
|
||
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE | ||
; | ||
|
||
public static RequestMethod from(final String value) { | ||
return Arrays.stream(values()) | ||
.filter(method -> method.name().equals(value.toUpperCase())) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("정의되지 않은 메서드입니다. [%s]", value))); | ||
} | ||
} |
Oops, something went wrong.