Skip to content
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단계] 채채 (신채원) 미션 제출합니다 #613

Merged
merged 12 commits into from
Oct 10, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.WebApplicationInitializer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

채채는 DispatcherServletDispatcherServletInitializer를 app 패키지에 그대로 두셨군요!
mvc 패키지로 이동하는 것도 고려를 해볼 수 있을 것 같은데, 채채가 DispatcherServletDispatcherServletInitializer를 app 패키지에 두신 이유가 궁금합니다!

Copy link
Author

@chaewon121 chaewon121 Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DispatcherServletInitializer가 app패키지에 의존하고있어서 따로 분리하지 못하였습니다..! 이부분에 대해서 조이는 어떻게 처리하셨나요!? 저는 이부분에대해서 전혀 감을 잡지 못하였습니다아...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 DispatcherServletInitializer는 app 패키지에 그냥 두고 DispatcherServlet만 이동했어요!
그냥 진짜 생각해보셨는지 궁금하기도 하고, 안 해보셨으면 해보면 좋을 것 같아서 말씀드렸어요🙂

import webmvc.org.springframework.web.servlet.mvc.asis.ControllerHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionHandlerAdapter;

Expand All @@ -21,9 +21,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) {
final DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.addHandlerMapping(new ManualHandlerMapping());
dispatcherServlet.addHandlerMapping(new AnnotationHandlerMapping("com"));
dispatcherServlet.addHandlerAdapter(new ControllerHandlerAdapter());
dispatcherServlet.addHandlerAdapter(new HandlerExecutionHandlerAdapter());

final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet);
Expand Down
40 changes: 0 additions & 40 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

19 changes: 19 additions & 0 deletions app/src/main/java/com/techcourse/controller/IndexController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 IndexController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(final HttpServletRequest req,
final HttpServletResponse res) {
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요기도 request, response로 통일해주면 좋을 것 같네용

return new ModelAndView(new JspView("index.jsp"));
}
}
34 changes: 20 additions & 14 deletions app/src/main/java/com/techcourse/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,45 @@

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.mvc.asis.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import webmvc.org.springframework.web.servlet.view.JspView;

public class LoginController implements Controller {
@Controller
public class LoginController {

private static final Logger log = LoggerFactory.getLogger(LoginController.class);

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) {
if (UserSession.isLoggedIn(req.getSession())) {

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(final HttpServletRequest request,
final HttpServletResponse response) {
final String path = execute(request, response);
return new ModelAndView(new JspView(path));
}

private String execute(final HttpServletRequest request,
final HttpServletResponse response) {
if (UserSession.isLoggedIn(request.getSession())) {
return "redirect:/index.jsp";
}

return InMemoryUserRepository.findByAccount(req.getParameter("account"))
return InMemoryUserRepository.findByAccount(request.getParameter("account"))
.map(user -> {
log.info("User : {}", user);
return login(req, user);
return login(request, user);
})
.orElse("redirect:/401.jsp");
}

private String login(final HttpServletRequest request, final User user) {
private String login(final HttpServletRequest request,
final User user) {
if (user.checkPassword(request.getParameter("password"))) {
final var session = request.getSession();
session.setAttribute(UserSession.SESSION_KEY, user);
Expand All @@ -39,8 +49,4 @@ private String login(final HttpServletRequest request, final User user) {
return "redirect:/401.jsp";
}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(HttpServletRequest req, HttpServletResponse res) {
return new ModelAndView(new JspView(execute(req, res)));
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
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 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.mvc.asis.Controller;
import webmvc.org.springframework.web.servlet.view.JspView;

public class LoginViewController implements Controller {
@Controller
public class LoginViewController {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Login 관련 메서드들을 하나의 컨트롤러에서 관리할수도 있을 것 같은데 LoginControllerLoginViewController로 구분하신 기준이 궁금합니다!


private static final Logger log = LoggerFactory.getLogger(LoginViewController.class);

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) {
return UserSession.getUserFrom(req.getSession())
@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView loginView(final HttpServletRequest request,
final HttpServletResponse response) {
final String path = execute(request, response);
return new ModelAndView(new JspView(path));
}

private String execute(final HttpServletRequest request,
final HttpServletResponse response) {
return UserSession.getUserFrom(request.getSession())
.map(user -> {
log.info("logged in {}", user.getAccount());
return "redirect:/index.jsp";
})
.orElse("/login.jsp");
}

@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) {
String path = execute(req, res);
return new ModelAndView(new JspView(path));
}
}
19 changes: 7 additions & 12 deletions app/src/main/java/com/techcourse/controller/LogoutController.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
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.mvc.asis.Controller;
import webmvc.org.springframework.web.servlet.view.JspView;

public class LogoutController implements Controller {
@Controller
public class LogoutController {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로그아웃 기능이 정상 작동 하지 않는 것 같아요~ 확인 한번 해보시면 좋을 것 같아요!
(아마 request method 때문인 것 같네요)

Copy link
Author

@chaewon121 chaewon121 Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

post로 되어있군요..ㅠㅠ 수정하겠습니다~!


@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) {
final var session = req.getSession();
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
final var session = request.getSession();
session.removeAttribute(UserSession.SESSION_KEY);
return "redirect:/";
}

@RequestMapping(value = "/logout", method = RequestMethod.POST)
public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) {
String path = execute(req, res);
return new ModelAndView(new JspView(path));
return new ModelAndView(new JspView("redirect:/"));
}
}
29 changes: 12 additions & 17 deletions app/src/main/java/com/techcourse/controller/RegisterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@

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 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.mvc.asis.Controller;
import webmvc.org.springframework.web.servlet.view.JspView;

public class RegisterController implements Controller {

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
final var user = new User(2,
req.getParameter("account"),
req.getParameter("password"),
req.getParameter("email"));
InMemoryUserRepository.save(user);

return "redirect:/index.jsp";
}
@Controller
public class RegisterController {

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
String path = execute(request, response);
return new ModelAndView(new JspView(path));
public ModelAndView save(final HttpServletRequest request,
final HttpServletResponse response) {
final User user = new User(2,
request.getParameter("account"),
request.getParameter("password"),
request.getParameter("email"));
InMemoryUserRepository.save(user);
final JspView view = new JspView("redirect:/index.jsp");
return new ModelAndView(view);
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
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.mvc.asis.Controller;
import webmvc.org.springframework.web.servlet.view.JspView;

public class RegisterViewController implements Controller {

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) {
return "/register.jsp";
}
@Controller
public class RegisterViewController {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마찬가지로 RegisterViewController와 RegisterController를 분리하신 이유가 궁금합니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 위 3개의 질문에 대해서 뷰를 분리한 이유는 사실 크게 없는데..요청 uri가 달라서 분리를 했습니다!


@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) {
String path = execute(req, res);
return new ModelAndView(new JspView(path));
public ModelAndView loginView(final HttpServletRequest req,
final HttpServletResponse res) {
return new ModelAndView(new JspView("/register.jsp"));
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/techcourse/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
Expand Up @@ -39,8 +39,8 @@ public void init(final FilterConfig filterConfig) throws ServletException {
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
throws IOException, ServletException {
final var req = (HttpServletRequest) request;
final var path = req.getRequestURI().substring(req.getContextPath().length());
final var httpServletRequest = (HttpServletRequest) request;
final var path = httpServletRequest.getRequestURI().substring(httpServletRequest.getContextPath().length());
if (isResourceUrl(path)) {
log.debug("path : {}", path);
requestDispatcher.forward(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public ModelAndView(final View view) {
this.model = new HashMap<>();
}

public ModelAndView addObject(final String attributeName, final Object attributeValue) {
public ModelAndView addObject(final String attributeName,
final Object attributeValue) {
model.put(attributeName, attributeValue);
return this;
Comment on lines +17 to 20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

빌더 패턴 👍

}
Expand Down
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;
Expand All @@ -8,8 +8,6 @@
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.HandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.HandlerMapping;

public class DispatcherServlet extends HttpServlet {

Expand Down Expand Up @@ -41,9 +39,9 @@ protected void service(final HttpServletRequest request, final HttpServletRespon
}
}

private void render(ModelAndView modelAndView, HttpServletRequest req, HttpServletResponse res) throws Exception {
View view = modelAndView.getView();
view.render(modelAndView.getModel(), req, res);
private void render(ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response) throws Exception {
final View view = modelAndView.getView();
view.render(modelAndView.getModel(), request, response);
}

public void addHandlerMapping(HandlerMapping handlerMapping) {
Expand Down
Loading
Loading