-
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단계] 하마드(이건회) 미션 제출합니다! ⚽️ (#505)
* refactor : 인스턴스화시 예외처리 런타임으로 변환 * refactor : RequestMethod 변환 기능 내장함수로 대체 * feat : 어노테이션 기반 컨트롤러 공존 * refactor : swtich-case 기반 정적메소드 변경 * feat : 로그인, 회원가입 컨트롤러 어노테이션 기반으로 처리
- Loading branch information
1 parent
e888c2e
commit d64fa12
Showing
17 changed files
with
399 additions
and
85 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
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/handlerMapper/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 com.techcourse.handlerMapper; | ||
|
||
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.HandlerExecution; | ||
|
||
public class AnnotationHandlerAdapter implements HandlerAdapter { | ||
@Override | ||
public boolean supports(Object handler) { | ||
return handler instanceof HandlerExecution; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
return ((HandlerExecution) handler).handle(request, response); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
app/src/main/java/com/techcourse/handlerMapper/AnnotationHandlerMapping.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,62 @@ | ||
package com.techcourse.handlerMapper; | ||
|
||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.reflections.Reflections; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import web.org.springframework.web.bind.annotation.RequestMapping; | ||
import web.org.springframework.web.bind.annotation.RequestMethod; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerKey; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class AnnotationHandlerMapping implements HandlerMapping { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); | ||
|
||
private final Object[] basePackage; | ||
private final Map<HandlerKey, HandlerExecution> handlerExecutions; | ||
|
||
public AnnotationHandlerMapping(final Object... basePackage) { | ||
this.basePackage = basePackage; | ||
this.handlerExecutions = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
Reflections reflections = new Reflections(basePackage); | ||
Set<Class<?>> controllers = reflections.getTypesAnnotatedWith(Controller.class); | ||
for (Class<?> controller : controllers) { | ||
Method[] methods = controller.getDeclaredMethods(); | ||
for (Method method : methods) { | ||
if (method.isAnnotationPresent(RequestMapping.class)) { | ||
RequestMapping annotation = method.getAnnotation(RequestMapping.class); | ||
String url = annotation.value(); | ||
for (RequestMethod requestMethod : annotation.method()) { | ||
Object instance = null; | ||
try { | ||
instance = controller.getDeclaredConstructor().newInstance(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
HandlerKey handlerKey = new HandlerKey(url, requestMethod); | ||
HandlerExecution handlerExecution = new HandlerExecution(instance, method); | ||
handlerExecutions.put(handlerKey, handlerExecution); | ||
} | ||
} | ||
} | ||
} | ||
log.info("Initialized AnnotationHandlerMapping!"); | ||
} | ||
|
||
@Override | ||
public Object getHandler(final HttpServletRequest request) { | ||
HandlerKey handlerKey = new HandlerKey(request.getRequestURI(), RequestMethod.resolve(request.getMethod())); | ||
return handlerExecutions.get(handlerKey); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/techcourse/handlerMapper/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 com.techcourse.handlerMapper; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
public interface HandlerAdapter { | ||
|
||
boolean supports(Object handler); | ||
|
||
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/techcourse/handlerMapper/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 com.techcourse.handlerMapper; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public interface HandlerMapping { | ||
|
||
void initialize(); | ||
|
||
Object getHandler(HttpServletRequest request); | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/techcourse/handlerMapper/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,20 @@ | ||
package com.techcourse.handlerMapper; | ||
|
||
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(Object handler) { | ||
return handler instanceof Controller; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
String viewName = ((Controller) handler).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
Oops, something went wrong.