-
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단계] 헙크(정현승) 미션 제출합니다. (#384) * 패키지 위치 변경 및 코드 정리 * 서블릿 학습 테스트 코드 개선 * feat: 학습 테스트 구현 * test: Reflections 테스트 작성 * feat: AnnotationHandlerMapping 초안 * refactor: AnnotationHandlerMapping 수정 * test: Servlet 테스트 작성 --------- Co-authored-by: kang-hyungu <[email protected]> * feat: HandlerMapping 구현 * feat: HandlerMappings와 Adapter 구현 * feat: DispatcherServlet에 HandlerMappings, HandlerAdapters 적용 * feat: JspView 구현 * fix: HandlerExecution 수정 * refactor: 패키지 구조 수정 * refactor: 패키지 구조 수정 * fix: DispatcherServlet 클래스의 ModelAndView의 model 반환 로직 수정 * refactor: HandlerMappings 패키지 위치 수정 --------- Co-authored-by: kang-hyungu <[email protected]>
- Loading branch information
1 parent
1f3fc3a
commit e3ae1b7
Showing
13 changed files
with
220 additions
and
57 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
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/techcourse/controller/RegisterAnnotationController.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,31 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.techcourse.domain.User; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
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 RegisterAnnotationController { | ||
|
||
@RequestMapping(value = "/register-annotated", method = RequestMethod.POST) | ||
public ModelAndView save(final HttpServletRequest request, final HttpServletResponse response) { | ||
final var user = new User(2, | ||
request.getParameter("account"), | ||
request.getParameter("password"), | ||
request.getParameter("email")); | ||
InMemoryUserRepository.save(user); | ||
|
||
return new ModelAndView(new JspView("redirect:/index.jsp")); | ||
} | ||
|
||
@RequestMapping(value = "/register-annotated", method = RequestMethod.GET) | ||
public ModelAndView show(final HttpServletRequest request, final HttpServletResponse response) { | ||
return new ModelAndView(new JspView("register.jsp")); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...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,18 @@ | ||
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 Object handler, final HttpServletRequest request, final HttpServletResponse response) 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
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 supports(final Object handler); | ||
|
||
ModelAndView handle(final Object handler, final HttpServletRequest request, final HttpServletResponse response) throws Exception; | ||
} |
27 changes: 27 additions & 0 deletions
27
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.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,27 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class HandlerAdapters { | ||
private final List<HandlerAdapter> adapters; | ||
|
||
public HandlerAdapters() { | ||
this.adapters = new ArrayList<>(); | ||
} | ||
|
||
public void init() { | ||
adapters.add(new AnnotationHandlerAdapter()); | ||
adapters.add(new ManualHandlerAdapter()); | ||
} | ||
|
||
public Optional<HandlerAdapter> getAdapter(final Object handler) { | ||
for (final HandlerAdapter handlerAdapter : adapters) { | ||
if (handlerAdapter.supports(handler)) { | ||
return Optional.of(handlerAdapter); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
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
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(final HttpServletRequest request); | ||
} |
31 changes: 31 additions & 0 deletions
31
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.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,31 @@ | ||
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 HandlerMappings { | ||
private final List<HandlerMapping> mappings; | ||
|
||
public HandlerMappings() { | ||
this.mappings = new ArrayList<>(); | ||
} | ||
|
||
public void init(final HandlerMapping... mappings) { | ||
this.mappings.add(new AnnotationHandlerMapping()); | ||
this.mappings.addAll(List.of(mappings)); | ||
} | ||
|
||
public Optional<Object> getHandler(final HttpServletRequest request) { | ||
for (final HandlerMapping handlerMapping : mappings) { | ||
final Object handler = handlerMapping.getHandler(request); | ||
if (Objects.nonNull(handler)) { | ||
return Optional.of(handler); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/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 webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
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.view.JspView; | ||
|
||
public class ManualHandlerAdapter implements HandlerAdapter { | ||
@Override | ||
public boolean supports(final Object handler) { | ||
return handler instanceof Controller; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final Object handler, final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
final Controller controller = (Controller) handler; | ||
final String path = controller.execute(request, response); | ||
return new ModelAndView(new JspView(path)); | ||
} | ||
} |
Oops, something went wrong.