-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MVC 구현하기 2단계] 디투(박정훈) 미션 제출합니다. (#456)
* test: Reflection 새로운 방식으로 출력 테스트 * test: 불필요한 출력 제거 * refactor: 잘못된 핸들러는 무시하도록 설정 * feat: HandlerMapping 인터페이스 추가 * feat: 어노테이션 기반, 메뉴얼 기반 핸들러에 implements HandlerMapping * feat: HandlerExecutionHandlerAdapter 생성 * feat: HandlerMappings 일급 컬렉션 추가 * feat: HandlerAdapter 구현 * feat: Dazzle 컨트롤러 생성 * refactor: 예외 처리 못한 것 예외 처리
- Loading branch information
Showing
15 changed files
with
218 additions
and
21 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
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,26 @@ | ||
package com.techcourse; | ||
|
||
import webmvc.org.springframework.web.servlet.HandlerAdapter; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class HandlerAdapters { | ||
|
||
private final Set<HandlerAdapter> handlerAdapters; | ||
|
||
public HandlerAdapters() { | ||
this.handlerAdapters = new HashSet<>(); | ||
} | ||
|
||
public void addHandlerAdapter(final HandlerAdapter handlerAdapter) { | ||
handlerAdapters.add(handlerAdapter); | ||
} | ||
|
||
public HandlerAdapter getHandlerAdapter(final Object handler) { | ||
return handlerAdapters.stream() | ||
.filter(handlerAdapter -> handlerAdapter.supports(handler)) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("%s를 실행시킬 어댑터를 찾지 못했습니다.", 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,30 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import webmvc.org.springframework.web.servlet.HandlerMapping; | ||
|
||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class HandlerMappings { | ||
|
||
private final Set<HandlerMapping> handlerMappings; | ||
|
||
public HandlerMappings() { | ||
this.handlerMappings = new HashSet<>(); | ||
} | ||
|
||
public void addHandlerMapping(final HandlerMapping handlerMapping) { | ||
handlerMapping.initialize(); | ||
handlerMappings.add(handlerMapping); | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest req) { | ||
return handlerMappings.stream() | ||
.map(handlerMapping -> handlerMapping.getHandler(req)) | ||
.filter(Objects::nonNull) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("%s %s를 실행시킬 핸들러를 찾을 수 없습니다.", req.getMethod(), req.getRequestURI()))); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
package com.techcourse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.View; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class ManualHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(final Object handler) { | ||
return handler instanceof Controller; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final HttpServletRequest req, final HttpServletResponse res, final Object handler) throws Exception { | ||
final Controller controller = (Controller) handler; | ||
final String viewName = controller.execute(req, res); | ||
final View view = new JspView(viewName); | ||
return new ModelAndView(view); | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/techcourse/controller/DazzleController.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,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 DazzleController { | ||
|
||
@RequestMapping(value = "/dazzle", method = RequestMethod.GET) | ||
public ModelAndView showMyDazzle(final HttpServletRequest req, final HttpServletResponse res) { | ||
return new ModelAndView(new JspView("404.jsp")); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
mvc/src/main/java/webmvc/org/springframework/web/servlet/HandlerAdapter.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,11 @@ | ||
package webmvc.org.springframework.web.servlet; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
public interface HandlerAdapter { | ||
|
||
boolean supports(final Object handler); | ||
|
||
ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception; | ||
} |
10 changes: 10 additions & 0 deletions
10
mvc/src/main/java/webmvc/org/springframework/web/servlet/HandlerMapping.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,10 @@ | ||
package webmvc.org.springframework.web.servlet; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public interface HandlerMapping { | ||
|
||
void initialize(); | ||
|
||
Object getHandler(final HttpServletRequest req); | ||
} |
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
...c/main/java/webmvc/org/springframework/web/servlet/mvc/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 webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
public class AnnotationHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(final Object handler) { | ||
return handler instanceof HandlerExecution; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception { | ||
final HandlerExecution handlerExecution = (HandlerExecution) handler; | ||
return handlerExecution.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
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