-
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단계] 로이(김덕우) 미션 제출합니다. (#520)
* feat: 핸들러 매핑 추상화 * feat: 핸들러 어댑터 인터페이스 및 구현체 생성 * feat: HandlerAdapterRegistry 및 HandlerMappingRegistry 구현 * feat: DispatcherServlet 내 핸들러매핑 및 핸들러어댑터 설정 * refactor: 메서드 파라미터 리팩터링
- Loading branch information
Showing
11 changed files
with
185 additions
and
14 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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
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.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class ManualHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean isSupport(Object handler) { | ||
return handler instanceof Controller; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
Controller controller = (Controller) handler; | ||
String viewName = controller.execute(request, response); | ||
return new ModelAndView(new JspView(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
19 changes: 19 additions & 0 deletions
19
...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,19 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
public class AnnotationHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean isSupport(Object handler) { | ||
return handler instanceof HandlerExecution; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
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
12 changes: 12 additions & 0 deletions
12
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/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,12 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
public interface HandlerAdapter { | ||
|
||
boolean isSupport(Object handler); | ||
|
||
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; | ||
} |
26 changes: 26 additions & 0 deletions
26
...src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapterRegistry.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,26 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HandlerAdapterRegistry { | ||
|
||
private final List<HandlerAdapter> handlerAdapters; | ||
|
||
public HandlerAdapterRegistry() { | ||
this.handlerAdapters = new ArrayList<>(); | ||
} | ||
|
||
public void addHandlerAdapter(HandlerAdapter handlerAdapter) { | ||
this.handlerAdapters.add(handlerAdapter); | ||
} | ||
|
||
public HandlerAdapter getHandlerAdapter(Object handler) { | ||
for (HandlerAdapter handlerAdapter : this.handlerAdapters) { | ||
if (handlerAdapter.isSupport(handler)) { | ||
return handlerAdapter; | ||
} | ||
} | ||
throw new IllegalArgumentException("해당 handler를 찾을 수 없습니다."); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/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.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public interface HandlerMapping { | ||
|
||
void initialize(); | ||
|
||
Object getHandler(HttpServletRequest request); | ||
} |
34 changes: 34 additions & 0 deletions
34
...src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappingRegistry.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 webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class HandlerMappingRegistry { | ||
|
||
private final List<HandlerMapping> handlerMappings; | ||
|
||
public HandlerMappingRegistry() { | ||
this.handlerMappings = new ArrayList<>(); | ||
} | ||
|
||
public void init() { | ||
for (HandlerMapping handlerMapping : this.handlerMappings) { | ||
handlerMapping.initialize(); | ||
} | ||
} | ||
|
||
public void addHandlerMapping(HandlerMapping handlerMapping) { | ||
handlerMappings.add(handlerMapping); | ||
} | ||
|
||
public Optional<Object> getHandler(HttpServletRequest request) { | ||
return handlerMappings.stream() | ||
.map(handlerMapping -> handlerMapping.getHandler(request)) | ||
.filter(Objects::nonNull) | ||
.findFirst(); | ||
} | ||
} |
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