-
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 구현하기 - 2단계] 제이미(임정수) 미션 제출합니다. (#492)
* feat: Legacy MVC와 @MVC 통합하기 * feat: url을 찾을 수 없는 경우의 예외 처리 추가 * refactor: 메서드 명을 명확하게 수정 * refactor: handler의 인스턴스를 주입 받도록 수정 * refactor: static 제거 * refactor: 기존 코드로 돌려 놓기 * refactor: 특정 컨트롤러만 어노테이션 기반 컨트롤러로 변경 * feat: 핸들러 어뎁터 추가 * feat: 핸들러 매핑 추가 * feat: DispatcherServlet과의 연결 * rename: 패키지 위치 변경 * refacotr: map의 getOrDefault()를 사용하도록 수정
- Loading branch information
Showing
18 changed files
with
240 additions
and
66 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
20 changes: 13 additions & 7 deletions
20
app/src/main/java/com/techcourse/ManualHandlerMapping.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,35 +1,41 @@ | ||
package com.techcourse; | ||
|
||
import com.techcourse.controller.*; | ||
import com.techcourse.controller.LoginController; | ||
import com.techcourse.controller.LoginViewController; | ||
import com.techcourse.controller.LogoutController; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ManualHandlerMapping { | ||
public class ManualHandlerMapping implements HandlerMapping { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ManualHandlerMapping.class); | ||
|
||
private static final Map<String, Controller> controllers = new HashMap<>(); | ||
private final Map<String, Controller> controllers = new HashMap<>(); | ||
|
||
@Override | ||
public void initialize() { | ||
controllers.put("/", new ForwardController("/index.jsp")); | ||
controllers.put("/login", new LoginController()); | ||
controllers.put("/login/view", new LoginViewController()); | ||
controllers.put("/logout", new LogoutController()); | ||
controllers.put("/register/view", new RegisterViewController()); | ||
controllers.put("/register", new RegisterController()); | ||
|
||
log.info("Initialized Handler Mapping!"); | ||
controllers.keySet() | ||
.forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); | ||
} | ||
|
||
public Controller getHandler(final String requestURI) { | ||
@Override | ||
public Object getHandler(final HttpServletRequest request) { | ||
final String requestURI = request.getRequestURI(); | ||
log.debug("Request Mapping Uri : {}", requestURI); | ||
return controllers.get(requestURI); | ||
|
||
return controllers.getOrDefault(requestURI, new ForwardController("/404.jsp")); | ||
} | ||
} |
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
13 changes: 0 additions & 13 deletions
13
app/src/main/java/com/techcourse/controller/RegisterViewController.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/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; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
public interface HandlerAdapter { | ||
|
||
boolean isSupport(final Object object); | ||
|
||
ModelAndView handle(final Object object, final HttpServletRequest request, final HttpServletResponse response) throws Exception; | ||
} |
22 changes: 22 additions & 0 deletions
22
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/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,22 @@ | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import webmvc.org.springframework.web.servlet.mvc.exception.NotFoundHandlerAdapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HandlerAdapterRegistry { | ||
|
||
final private List<HandlerAdapter> handlerAdapters = new ArrayList<>(); | ||
|
||
public void addHandlerAdapter(final HandlerAdapter handlerAdapter) { | ||
handlerAdapters.add(handlerAdapter); | ||
} | ||
|
||
public HandlerAdapter getHandlerAdapter(final Object object) { | ||
return handlerAdapters.stream() | ||
.filter(handlerAdapter -> handlerAdapter.isSupport(object)) | ||
.findFirst() | ||
.orElseThrow(() -> new NotFoundHandlerAdapter("해당 핸들러 어댑터를 찾지 못했습니다.")); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/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; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public interface HandlerMapping { | ||
|
||
void initialize(); | ||
|
||
Object getHandler(final HttpServletRequest request); | ||
} |
29 changes: 29 additions & 0 deletions
29
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/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,29 @@ | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import webmvc.org.springframework.web.servlet.mvc.exception.NotFoundHandlerMapping; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class HandlerMappingRegistry { | ||
|
||
final private List<HandlerMapping> handlerMappings = new ArrayList<>(); | ||
|
||
public void initialize() { | ||
handlerMappings.forEach(HandlerMapping::initialize); | ||
} | ||
|
||
public void addHandlerMapping(final HandlerMapping handlerMapping) { | ||
handlerMappings.add(handlerMapping); | ||
} | ||
|
||
public Object getHandler(HttpServletRequest request) { | ||
return handlerMappings.stream() | ||
.map(handlerMapping -> handlerMapping.getHandler(request)) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElseThrow(() -> new NotFoundHandlerMapping("해당 핸들러 매핑을 찾지 못했습니다.")); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...c/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.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,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.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class ControllerHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean isSupport(final Object object) { | ||
return object instanceof Controller; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(final Object object, final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
final Controller controller = (Controller) object; | ||
final String path = controller.execute(request, response); | ||
|
||
return new ModelAndView(new JspView(path)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ain/java/webmvc/org/springframework/web/servlet/mvc/exception/NotFoundHandlerAdapter.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,8 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.exception; | ||
|
||
public class NotFoundHandlerAdapter extends IllegalArgumentException { | ||
|
||
public NotFoundHandlerAdapter(final String message) { | ||
super(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ain/java/webmvc/org/springframework/web/servlet/mvc/exception/NotFoundHandlerMapping.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,8 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.exception; | ||
|
||
public class NotFoundHandlerMapping extends IllegalArgumentException { | ||
|
||
public NotFoundHandlerMapping(final String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.