-
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 구현하기 - 3단계] 밀리(김미성) 미션 제출합니다. #630
Changes from 9 commits
5780286
31952ac
839d97c
e2ee628
48b38c9
d2a8bbc
3ca34d7
e510279
e0ca93f
0c30f71
dca0adb
a3fc8c2
206f941
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.techcourse.controller; | ||
|
||
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; | ||
|
||
@Controller | ||
public class ForwardController { | ||
|
||
private static final String PATH = "/index.jsp"; | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public String execute(final HttpServletRequest request, final HttpServletResponse response) { | ||
return PATH; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 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.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JsonView; | ||
|
||
@Controller | ||
public class UserController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserController.class); | ||
|
||
@RequestMapping(value = "/api/user", method = RequestMethod.GET) | ||
public ModelAndView show(final HttpServletRequest request, final HttpServletResponse response) { | ||
final String account = request.getParameter("account"); | ||
log.debug("user id : {}", account); | ||
|
||
final ModelAndView modelAndView = new ModelAndView(new JsonView()); | ||
final User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(); | ||
|
||
modelAndView.addObject("user", user); | ||
return modelAndView; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.techcourse; | ||
package webmvc.org.springframework.web.servlet.mvc; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServlet; | ||
|
@@ -7,6 +7,7 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
@@ -17,7 +18,10 @@ | |
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapterException; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappingException; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.ManualHandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.JsonViewResolver; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.JspViewResolver; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.ViewResolver; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.ViewResolverException; | ||
|
||
public class DispatcherServlet extends HttpServlet { | ||
|
||
|
@@ -26,29 +30,31 @@ public class DispatcherServlet extends HttpServlet { | |
|
||
private final List<HandlerMapping> handlerMappings = new ArrayList<>(); | ||
private final List<HandlerAdapter> handlerAdapters = new ArrayList<>(); | ||
|
||
public DispatcherServlet() { | ||
} | ||
private final List<ViewResolver> viewResolvers = new ArrayList<>(); | ||
|
||
@Override | ||
public void init() { | ||
initHandlerMappings(); | ||
initHandlerAdapters(); | ||
initViewResolvers(); | ||
} | ||
|
||
private void initHandlerMappings() { | ||
handlerMappings.add(new WrappedManualHandlerMapping()); | ||
handlerMappings.add(new AnnotationHandlerMapping(getClass().getPackageName())); | ||
handlerMappings.add(new AnnotationHandlerMapping()); | ||
for (final HandlerMapping handlerMapping : handlerMappings) { | ||
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. 오... AnnotationHandlerMapping 생성할때 basePakage명을 명시해주지 않아도 되는군요...!! DispatcherServletInitializer 클래스 기준으로 만들어 주는 걸까요??? 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. 넵 명시해주지 않아도 |
||
handlerMapping.initialize(); | ||
} | ||
} | ||
|
||
private void initHandlerAdapters() { | ||
handlerAdapters.add(new ManualHandlerAdapter()); | ||
handlerAdapters.add(new AnnotationHandlerAdapter()); | ||
} | ||
|
||
private void initViewResolvers() { | ||
viewResolvers.add(new JspViewResolver()); | ||
viewResolvers.add(new JsonViewResolver()); | ||
} | ||
|
||
@Override | ||
protected void service(final HttpServletRequest request, final HttpServletResponse response) | ||
throws ServletException { | ||
|
@@ -59,9 +65,11 @@ protected void service(final HttpServletRequest request, final HttpServletRespon | |
final Object handler = getHandler(request); | ||
final HandlerAdapter handlerAdapter = getHandlerAdapter(handler); | ||
final ModelAndView modelAndView = handlerAdapter.handle(handler, request, response); | ||
final View view = modelAndView.getView(); | ||
final View view = getView(modelAndView); | ||
final Map<String, Object> model = modelAndView.getModel(); | ||
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. HandlerExecution 클래스 리뷰에 남긴것처럼 수정 된다면, 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. 저는 뷰 리졸버가 있는게 훨씬 확장성이 좋다고 생각해서 그대로 뒀습니다..!! |
||
view.render(model, request, response); | ||
} catch (final HandlerMappingException | ViewResolverException e) { | ||
throw e; | ||
} catch (final Throwable e) { | ||
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. JspView로 404.jsp를 내려주는 것을 말씀하시는 건가요?! 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. response에 뷰를 그려주는것이 아니라, |
||
log.error("Exception : {}", e.getMessage(), e); | ||
throw new ServletException(e.getMessage()); | ||
|
@@ -70,9 +78,9 @@ protected void service(final HttpServletRequest request, final HttpServletRespon | |
|
||
private Object getHandler(final HttpServletRequest request) { | ||
for (final HandlerMapping handlerMapping : handlerMappings) { | ||
final Object handler = handlerMapping.getHandler(request); | ||
if (handler != null) { | ||
return handler; | ||
final Optional<Object> handler = handlerMapping.getHandler(request); | ||
if (handler.isPresent()) { | ||
return handler.get(); | ||
} | ||
} | ||
throw new HandlerMappingException("해당 요청에 대한 핸들러가 없습니다."); | ||
|
@@ -86,4 +94,13 @@ private HandlerAdapter getHandlerAdapter(final Object handler) { | |
} | ||
throw new HandlerAdapterException("해당 요청에 대한 핸들러 어댑터가 없습니다."); | ||
} | ||
|
||
private View getView(final ModelAndView modelAndView) { | ||
final Object view = modelAndView.getView(); | ||
return viewResolvers.stream() | ||
.filter(viewResolver -> viewResolver.supports(view)) | ||
.findFirst() | ||
.map(viewResolver -> viewResolver.resolve(view)) | ||
.orElseThrow(() -> new ViewResolverException("해당 요청에 대한 view를 처리할 수 없습니다.")); | ||
} | ||
} |
This file was deleted.
This file was deleted.
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.
이 필드들 일급 컬렉션으로 관리하면 DispatcherServlet에서 service 메서드에 집중할 수 있을 것 같아요 !
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.
수정했습니다! 확실히 코드가 깔끔해졌네요 ㅎㅎ