Skip to content

Commit

Permalink
[MVC 구현 3단계] 헙크(정현승) 미션 제출합니다. (#550)
Browse files Browse the repository at this point in the history
* 패키지 위치 변경 및 코드 정리

* 서블릿 학습 테스트 코드 개선

* [MVC 구현하기 - 1단계] 헙크(정현승) 미션 제출합니다. (#384)

* 패키지 위치 변경 및 코드 정리

* 서블릿 학습 테스트 코드 개선

* feat: 학습 테스트 구현

* test: Reflections 테스트 작성

* feat: AnnotationHandlerMapping 초안

* refactor: AnnotationHandlerMapping 수정

* test: Servlet 테스트 작성

---------

Co-authored-by: kang-hyungu <[email protected]>

* [MVC 2단계] 헙크 미션 제출합니다 (#449)

* 패키지 위치 변경 및 코드 정리

* 서블릿 학습 테스트 코드 개선

* [MVC 구현하기 - 1단계] 헙크(정현승) 미션 제출합니다. (#384)

* 패키지 위치 변경 및 코드 정리

* 서블릿 학습 테스트 코드 개선

* feat: 학습 테스트 구현

* test: Reflections 테스트 작성

* feat: AnnotationHandlerMapping 초안

* refactor: AnnotationHandlerMapping 수정

* test: Servlet 테스트 작성

---------

Co-authored-by: kang-hyungu <[email protected]>

* feat: HandlerMapping 구현

* feat: HandlerMappings와 Adapter 구현

* feat: DispatcherServlet에 HandlerMappings, HandlerAdapters 적용

* feat: JspView 구현

* fix: HandlerExecution 수정

* refactor: 패키지 구조 수정

* refactor: 패키지 구조 수정

* fix: DispatcherServlet 클래스의 ModelAndView의 model 반환 로직 수정

* refactor: HandlerMappings 패키지 위치 수정

---------

Co-authored-by: kang-hyungu <[email protected]>

* feat: JsonView 구현

* feat: 어노테이션 기반 MVC로 수정 및 DispatcherServlet 패키지 위치 이동

* refactor: JsonView에 try-with-resources 구문 적용

* refactor: tobe 패키지 구조화

* feat: Stage3, DiContainer 구현

* feat: Stage4, DiContainer 구현

* fix: JsonView의 model 사이즈에 따른 응답값 구성 로직 수정

* refactor: RegisterController의 중복 핸들러 제거 및 예외 처리 수정

---------

Co-authored-by: kang-hyungu <[email protected]>
  • Loading branch information
HubCreator and kang-hyungu authored Sep 25, 2023
1 parent e3ae1b7 commit cc78e76
Show file tree
Hide file tree
Showing 27 changed files with 255 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.WebApplicationInitializer;
import webmvc.org.springframework.web.servlet.mvc.tobe.DispatcherServlet;

/**
* Base class for {@link WebApplicationInitializer}
Expand Down
47 changes: 0 additions & 47 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

18 changes: 18 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,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 IndexController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(new JspView("index.jsp"));
}
}
44 changes: 31 additions & 13 deletions app/src/main/java/com/techcourse/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,54 @@

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 webmvc.org.springframework.web.servlet.mvc.asis.Controller;
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.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) throws Exception {
if (UserSession.isLoggedIn(req.getSession())) {
return "redirect:/index.jsp";
}
@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView showLogin(HttpServletRequest request, HttpServletResponse response) {
return UserSession.getUserFrom(request.getSession())
.map(user -> {
log.info("logged in {}", user.getAccount());
return show("redirect:/index.jsp");
})
.orElse(show("/login.jsp"));
}

return InMemoryUserRepository.findByAccount(req.getParameter("account"))
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
if (UserSession.isLoggedIn(request.getSession())) {
return show("redirect:/index.jsp");
}
return InMemoryUserRepository.findByAccount(request.getParameter("account"))
.map(user -> {
log.info("User : {}", user);
return login(req, user);
return login(request, user);
})
.orElse("redirect:/401.jsp");
.orElse(show("redirect:/401.jsp"));
}

private String login(final HttpServletRequest request, final User user) {
private ModelAndView login(final HttpServletRequest request, final User user) {
if (user.checkPassword(request.getParameter("password"))) {
final var session = request.getSession();
session.setAttribute(UserSession.SESSION_KEY, user);
return "redirect:/index.jsp";
return show("redirect:/index.jsp");
}
return "redirect:/401.jsp";
return show("redirect:/401.jsp");
}

private ModelAndView show(final String viewName) {
return new ModelAndView(new JspView(viewName));
}
}

This file was deleted.

17 changes: 11 additions & 6 deletions app/src/main/java/com/techcourse/controller/LogoutController.java
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 {
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:/";
return new ModelAndView(new JspView("redirect:/"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@

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 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 RegisterController implements Controller {
@Controller
public class RegisterController {

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public ModelAndView showRegister(HttpServletRequest request, HttpServletResponse response) {
return show("/register.jsp");
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView register(HttpServletRequest request, final HttpServletResponse response) {
final var user = new User(2,
req.getParameter("account"),
req.getParameter("password"),
req.getParameter("email"));
request.getParameter("account"),
request.getParameter("password"),
request.getParameter("email"));
InMemoryUserRepository.save(user);

return "redirect:/index.jsp";
return show("redirect:/index.jsp");
}

private ModelAndView show(final String viewName) {
return new ModelAndView(new JspView(viewName));
}
}

This file was deleted.

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(HttpServletRequest request, HttpServletResponse response) {
final String account = request.getParameter("account");
log.info("user id : {}", account);

final ModelAndView modelAndView = new ModelAndView(new JsonView());
final User user = InMemoryUserRepository.findByAccount(account)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 사용자입니다."));

modelAndView.addObject("user1", user);
// modelAndView.addObject("user2", user);
// modelAndView.addObject("user3", "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.tobe;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
Expand All @@ -7,9 +7,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapters;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappings;
import webmvc.org.springframework.web.servlet.mvc.tobe.handleradapter.HandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.handleradapter.HandlerAdapters;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlermapping.HandlerMappings;

import java.util.Optional;

Expand All @@ -28,7 +28,7 @@ public DispatcherServlet() {

@Override
public void init() {
handlerMappings.init(new ManualHandlerMapping());
handlerMappings.init();
handlerAdapters.init();
}

Expand All @@ -43,7 +43,7 @@ protected void service(final HttpServletRequest request, final HttpServletRespon
}
final Object handler = handlerOptional.get();
final HandlerAdapter handlerAdapter = handlerAdapters.getAdapter(handler)
.orElseThrow(() -> new IllegalStateException("핸들러 어댑터를 찾을 수 없습니다."));
.orElseThrow(() -> new UnsupportedOperationException("핸들러 어댑터를 찾을 수 없습니다."));
final ModelAndView modelAndView = handlerAdapter.handle(handler, request, response);
modelAndView.getView().render(modelAndView.getModel(), request, response);
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc.tobe.handleradapter;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlermapping.HandlerExecution;

public class AnnotationHandlerAdapter implements HandlerAdapter {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc.tobe.handleradapter;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc.tobe.handleradapter;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc.tobe.handleradapter;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlermapping;

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlermapping;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlermapping;

import web.org.springframework.web.bind.annotation.RequestMethod;

Expand Down
Loading

0 comments on commit cc78e76

Please sign in to comment.