-
Notifications
You must be signed in to change notification settings - Fork 302
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단계] 로지(윤가영) 미션 제출합니다. #564
Changes from 10 commits
46c696f
f588d93
3f64b16
a1ea682
13494f4
a89ed26
6396d4e
50677b6
a42bd65
7f3363e
663a126
c970c11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Controller | ||
public class UserController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserController.class); | ||
|
||
@RequestMapping(value = "/api/user", method = RequestMethod.GET) | ||
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { | ||
final String account = requireNonNull(request.getParameter("account"), "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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.techcourse.controller; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class UserControllerTest { | ||
|
||
private UserController userController = new UserController(); | ||
@Test | ||
void account가_없으면_예외가_발생한다() { | ||
//given | ||
final HttpServletRequest mockRequest = mock(HttpServletRequest.class); | ||
final HttpServletResponse mockResponse = mock(HttpServletResponse.class); | ||
when(mockRequest.getParameter("account")).thenReturn(null); | ||
|
||
//when | ||
//then | ||
assertThatThrownBy(() -> userController.show(mockRequest, mockResponse)) | ||
.isInstanceOf(NullPointerException.class) | ||
.hasMessage("account를 입력해주세요."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,13 @@ | |
import org.slf4j.LoggerFactory; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.View; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; | ||
kyY00n marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public class DispatcherServlet extends HttpServlet { | ||
|
||
private static final long serialVersionUID = 1L; | ||
private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); | ||
|
||
private List<HandlerMapping> handlerMappings; | ||
private final List<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. 일...급..컬랙션..?! 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. 반영했습니다! 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. 확인했습니다! |
||
|
||
public DispatcherServlet(ServletContext servletContext) { | ||
handlerMappings = (List<HandlerMapping>) servletContext.getAttribute("handlerMappings"); | ||
|
@@ -40,7 +38,7 @@ private void handle(final HttpServletRequest request, final HttpServletResponse | |
throws Exception { | ||
final HandlerExecution handler = getHandler(request); | ||
final ModelAndView modelAndView = handler.handle(request, response); | ||
render(request, response, modelAndView); | ||
modelAndView.render(request, response); | ||
} | ||
|
||
private HandlerExecution getHandler(final HttpServletRequest request) throws ServletException { | ||
|
@@ -51,14 +49,4 @@ private HandlerExecution getHandler(final HttpServletRequest request) throws Ser | |
.orElseThrow(() -> new ServletException("핸들러를 찾을 수 없습니다.")); | ||
} | ||
|
||
private void render(final HttpServletRequest request, final HttpServletResponse response, | ||
final ModelAndView modelAndView) throws Exception { | ||
final View view = modelAndView.getView(); | ||
if (view.isRedirectView()) { | ||
response.sendRedirect(view.getViewName()); | ||
return; | ||
} | ||
view.render(modelAndView.getModel(), request, response); | ||
} | ||
|
||
} |
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.
오홍
DispatcherServletInitializer
에서AppInitializer
으로 이름을 바꾼 이유가 있을까유?!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이 아니라 App으로 변경하면 app 모듈에 조금 더 직관적이지 않을까? 생각했어요.
애초에 저 이름이 있었어서 계속 mvc 모듈로 옮기려고 집착한 것 같다는 이유도 있습니다.. 하하.