-
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단계] 루쿠(백경환) 미션 제출합니다. #621
Changes from all commits
173fbf8
5297647
4aecbb2
1f4d62a
20072df
cd63445
9fd39f2
94d6bb7
f4dbb86
cec9ea3
8ada904
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,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; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
@Controller | ||
public class ForwardController { | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView execute(final HttpServletRequest request, final HttpServletResponse response) { | ||
return new ModelAndView(new JspView("/index.jsp")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
package com.techcourse.controller; | ||
|
||
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 webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
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.JspView; | ||
|
||
public class LoginViewController implements Controller { | ||
@Controller | ||
public class LoginViewController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return UserSession.getUserFrom(req.getSession()) | ||
@RequestMapping(value = "/login/view", method = RequestMethod.GET) | ||
public ModelAndView getLoginView(final HttpServletRequest request, final HttpServletResponse response) { | ||
return UserSession.getUserFrom(request.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return "redirect:/index.jsp"; | ||
return new ModelAndView(new JspView("redirect:/index.jsp")); | ||
}) | ||
.orElse("/login.jsp"); | ||
.orElse(new ModelAndView(new JspView("/login.jsp"))); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
package com.techcourse.controller; | ||
|
||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
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.JspView; | ||
|
||
public class LogoutController implements Controller { | ||
@Controller | ||
public class LogoutController{ | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
final var session = req.getSession(); | ||
@RequestMapping(value = "/logout", method = RequestMethod.GET) | ||
public ModelAndView logout(final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
final var session = request.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return "redirect:/"; | ||
return new ModelAndView(new JspView("redirect:/")); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
package com.techcourse.controller; | ||
|
||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.Controller; | ||
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.JspView; | ||
|
||
public class RegisterViewController implements Controller { | ||
@Controller | ||
public class RegisterViewController { | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return "/register.jsp"; | ||
@RequestMapping(value = "/register/view", method = RequestMethod.GET) | ||
public ModelAndView getRegisterView(final HttpServletRequest request, final HttpServletResponse response) throws Exception { | ||
return new ModelAndView(new JspView("/register.jsp")); | ||
} | ||
} |
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 { | ||
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. 미션 힌트를 바탕으로 해당 컨트롤러를 추가해서 어노테이션 mvc가 정상적으로 동작하는지 테스트하기 위해 추가하였습니다~! |
||
|
||
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 = 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; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServlet; | ||
|
@@ -8,18 +8,16 @@ | |
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.ManualHandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class DispatcherServlet extends HttpServlet { | ||
|
||
private static final long serialVersionUID = 1L; | ||
private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); | ||
public static final String ANNOTATION_BASE_PACKAGE = "com.techcourse.controller"; | ||
private List<HandlerMapping> handlerMappings = new ArrayList<>(); | ||
private List<HandlerAdapter> handlerAdapters = new ArrayList<>(); | ||
|
||
|
@@ -28,12 +26,10 @@ public DispatcherServlet() { | |
|
||
@Override | ||
public void init() { | ||
handlerMappings.add(new ManualHandlerMapping()); | ||
handlerMappings.add(new AnnotationHandlerMapping("com.techcourse.controller")); | ||
handlerMappings.add(new AnnotationHandlerMapping(ANNOTATION_BASE_PACKAGE)); | ||
handlerMappings.forEach(HandlerMapping::initialize); | ||
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. 먼저 구조의 차이점에는 책임 분리가 되어있느냐 안되어있느냐 그 차이 인것 같아요 |
||
|
||
handlerAdapters.add(new AnnotationHandlerAdapter()); | ||
handlerAdapters.add(new ManualHandlerAdapter()); | ||
} | ||
|
||
@Override | ||
|
@@ -45,8 +41,8 @@ protected void service( | |
|
||
try { | ||
ModelAndView modelAndView = execute(request, response); | ||
String viewName = modelAndView.getView().getName(); | ||
move(viewName, request, response); | ||
View view = modelAndView.getView(); | ||
view.render(modelAndView.getModel(), request, response); | ||
} catch (Throwable e) { | ||
log.error("Exception : {}", e.getMessage(), e); | ||
throw new ServletException(e.getMessage()); | ||
|
@@ -57,31 +53,25 @@ private ModelAndView execute( | |
HttpServletRequest request, | ||
HttpServletResponse response | ||
) throws Exception { | ||
Object handler = handlerMappings.stream() | ||
.filter(handlerMapping -> handlerMapping.isMatch(request)) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException("지원하지 않는 요청입니다.")) | ||
.getHandler(request); | ||
Object handler = getHandler(request); | ||
HandlerAdapter adapter = getHandlerAdapter(handler); | ||
return adapter.handle(request, response, handler); | ||
} | ||
|
||
private HandlerAdapter getHandlerAdapter(Object handler) { | ||
HandlerAdapter adapter = handlerAdapters.stream() | ||
.filter(handlerAdapter -> handlerAdapter.supports(handler)) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException("지원하지 않는 요청입니다.")); | ||
|
||
return adapter.handle(request, response, handler); | ||
return adapter; | ||
} | ||
|
||
private void move( | ||
final String viewName, | ||
final HttpServletRequest request, | ||
final HttpServletResponse response | ||
) throws Exception { | ||
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); | ||
private Object getHandler(HttpServletRequest request) { | ||
Object handler = handlerMappings.stream() | ||
.filter(handlerMapping -> handlerMapping.isMatch(request)) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException("지원하지 않는 요청입니다.")) | ||
.getHandler(request); | ||
return handler; | ||
Comment on lines
+70
to
+74
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. 👍👍👍👍 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,5 +30,4 @@ public Map<String, Object> getModel() { | |
public View getView() { | ||
return view; | ||
} | ||
|
||
} |
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.
저는
JspView
에서는 JSP 파일로 이동시켜주는 역할만 해야 한다고 생각해서 redirect 는 다른 뷰로 분리하는 게 좋다고 생각해서 분리했었습니다! 이 부분에 대해 루쿠는 어떻게 생각하시나요? 😄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.
저도 redirect의 역할 분리를 위해 클래스를 생성 하는게 좋을거 같다는 쪽의 의견입니다!
근데
JspRedirectView
,JsonRedirectView
와 같은 클래스 분리를 떠올렸었는데이러면 품이 좀 들거같다는 생각이 들어서 고냥 이대로 진행하게 되었습니다~