-
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단계] 디투(박정훈) 미션 제출합니다. #625
Changes from all commits
5fe6ab3
43e0a2e
b5f2211
31b393b
1450f4c
e829dcd
f384e25
92a75c5
2cc9a45
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 |
---|---|---|
@@ -1,22 +1,27 @@ | ||
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 { | ||
@RequestMapping(value = "/login/view", method = RequestMethod.GET) | ||
public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return "redirect:/index.jsp"; | ||
return new ModelAndView(new JspView("/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,20 @@ | ||
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 { | ||
@RequestMapping(value = "/logout", method = RequestMethod.GET) | ||
public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
final var session = req.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 execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return new ModelAndView(new JspView("/register.jsp")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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(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,17 +1,17 @@ | ||
package com.techcourse; | ||
package webmvc.org.springframework.web.servlet; | ||
|
||
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. 과정을 하나씩 진행하면서 직접 겪었던 문제를 바탕으로 수정하신 점이 인상깊네요 👍 |
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import webmvc.org.springframework.web.servlet.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.View; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
import webmvc.org.springframework.web.servlet.adapter.HandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.adapter.HandlerAdapters; | ||
import webmvc.org.springframework.web.servlet.handlermapping.HandlerMappings; | ||
import webmvc.org.springframework.web.servlet.adapter.AnnotationHandlerAdapter; | ||
import webmvc.org.springframework.web.servlet.handlermapping.AnnotationHandlerMapping; | ||
import webmvc.org.springframework.web.servlet.view.View; | ||
|
||
public class DispatcherServlet extends HttpServlet { | ||
|
||
|
@@ -32,14 +32,12 @@ public void init() { | |
|
||
private void initHandlerMappings() { | ||
final HandlerMappings handlerMappings = new HandlerMappings(); | ||
handlerMappings.addHandlerMapping(new ManualHandlerMapping()); | ||
handlerMappings.addHandlerMapping(new AnnotationHandlerMapping()); | ||
this.handlerMappings = handlerMappings; | ||
} | ||
|
||
private void initHandlerAdapter() { | ||
final HandlerAdapters handlerAdapters = new HandlerAdapters(); | ||
handlerAdapters.addHandlerAdapter(new ManualHandlerAdapter()); | ||
handlerAdapters.addHandlerAdapter(new AnnotationHandlerAdapter()); | ||
this.handlerAdapters = handlerAdapters; | ||
} | ||
|
@@ -54,22 +52,10 @@ protected void service(final HttpServletRequest req, final HttpServletResponse r | |
final HandlerAdapter handlerAdapter = handlerAdapters.getHandlerAdapter(handler); | ||
final ModelAndView modelAndView = handlerAdapter.handle(req, res, handler); | ||
final View view = modelAndView.getView(); | ||
if (view instanceof JspView) { | ||
move(((JspView) view).getViewName(), req, res); | ||
} | ||
view.render(modelAndView.getModel(), req, res); | ||
} catch (Throwable e) { | ||
log.error("Exception : {}", e.getMessage(), e); | ||
throw new ServletException(e.getMessage()); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} |
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.
❤️