-
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단계] 호이(이건호) 미션 제출합니다. (#525)
- Loading branch information
Showing
14 changed files
with
227 additions
and
76 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
22 changes: 13 additions & 9 deletions
22
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,39 @@ | ||
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.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; | ||
|
||
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<>(); | ||
|
||
@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) { | ||
log.debug("Request Mapping Uri : {}", requestURI); | ||
return controllers.get(requestURI); | ||
@Override | ||
public Controller getHandler(final HttpServletRequest request) { | ||
log.debug("Request Mapping Uri : {}", request.getRequestURI()); | ||
return controllers.get(request.getRequestURI()); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/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,28 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.asis; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
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 void execute(final HttpServletRequest request, final HttpServletResponse response, final Object handler) | ||
throws Exception { | ||
final Controller controller = (Controller) handler; | ||
final String viewName = controller.execute(request, response); | ||
if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { | ||
response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); | ||
return; | ||
} | ||
|
||
final var requestDispatcher = request.getRequestDispatcher(viewName); | ||
requestDispatcher.forward(request, response); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...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,25 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.Map; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.View; | ||
|
||
public class AnnotationHandlerAdapter implements HandlerAdapter { | ||
|
||
@Override | ||
public boolean supports(final Object handler) { | ||
return handler instanceof HandlerExecution; | ||
} | ||
|
||
@Override | ||
public void execute(final HttpServletRequest request, final HttpServletResponse response, final Object handler) | ||
throws Exception { | ||
final HandlerExecution execution = (HandlerExecution) handler; | ||
final ModelAndView mav = execution.handle(request, response); | ||
final Map<String, Object> model = mav.getModel(); | ||
final View view = mav.getView(); | ||
view.render(model, 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
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
public interface HandlerAdapter { | ||
|
||
boolean supports(Object handler); | ||
|
||
void execute(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class HandlerAdapters { | ||
|
||
private Set<HandlerAdapter> values = new HashSet<>(); | ||
|
||
public void add(final HandlerAdapter handlerAdapter) { | ||
values.add(handlerAdapter); | ||
} | ||
|
||
public HandlerAdapter getHandlerAdapter(Object handler) { | ||
return values.stream() | ||
.filter(adapter -> adapter.supports(handler)) | ||
.findAny() | ||
.orElseThrow(); | ||
} | ||
} |
Oops, something went wrong.