-
Notifications
You must be signed in to change notification settings - Fork 305
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단계] 망고(고재철) 미션 제출합니다. #531
Changes from all commits
08458cc
9edd87c
2086b74
7ec3bd9
6c15068
a9282d2
2be6702
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.techcourse; | ||
|
||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HandlerAdapters { | ||
|
||
private final List<HandlerAdapter> adapters; | ||
|
||
public HandlerAdapters() { | ||
this.adapters = new ArrayList<>(); | ||
} | ||
|
||
public void addHandlerAdapter(final HandlerAdapter handlerAdapter) { | ||
adapters.add(handlerAdapter); | ||
} | ||
|
||
public HandlerAdapter getHandlerAdapter(final Object handler) { | ||
return adapters.stream() | ||
.filter(it -> it.supports(handler)) | ||
.findFirst() | ||
.orElseThrow(IllegalArgumentException::new); | ||
} | ||
} |
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.mvc.tobe.HandlerMapping; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class HandlerMappings { | ||
|
||
private final List<HandlerMapping> mappings; | ||
|
||
public HandlerMappings() { | ||
this.mappings = new ArrayList<>(); | ||
} | ||
|
||
public void addHandlerMapping(final HandlerMapping handlerMapping) { | ||
handlerMapping.initialize(); | ||
mappings.add(handlerMapping); | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
return mappings.stream() | ||
.map(it -> it.getHandler(request)) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElseThrow(IllegalStateException::new); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.asis; | ||
|
||
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.HandlerAdapter; | ||
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 request, final HttpServletResponse response, final Object handler) | ||
throws Exception { | ||
final var controller = (Controller) handler; | ||
final String viewName = controller.execute(request, response); | ||
return new ModelAndView(new JspView(viewName)); | ||
} | ||
} |
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.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 var handlerExecution = (HandlerExecution) handler; | ||
return handlerExecution.handle(request, response); | ||
} | ||
} |
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 supports(final Object handler); | ||
|
||
ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception; | ||
} |
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(final HttpServletRequest request); | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아마 Spring의 HandlerMapping에는 getHandler만이 존재하지만 이것을 없애기 위해서는 어댑터 패턴을 활용하면 해결하실 수 있을거에요~! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이 과정에서 오는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 스프링과 유사하게 initialize()를 생성자에서 호출하면 어떨까 하여 여쭌 것인데 |
||
} |
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.
transient
를 선택하신 이유는 무엇일까요~?혹시
Servlet
이Serializable
한 것과 관계가 있을까요?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.
넵! 이 부분은 Serializable 클래스는 필드들도 Serializable or transient 해야한다는 SonarLint의 조언을 보고 적용했습니다.
HandlerMappings
와HandlerAdapters
는 직렬화 해줄 필요성을 못느껴서 transient로 두었습니다!