Skip to content

Commit

Permalink
[MVC 구현하기 - 3단계] 하마드(이건회) 미션 제출합니다. (#548)
Browse files Browse the repository at this point in the history
* 패키지 위치 변경 및 코드 정리

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

* chore: 충돌 해결

* refactor: Mapper 관련 클래스 mvc 패키지로 이동

* refactor: 어노테이션 기반 컨트롤러로 대체

* feat: json타입 직렬화

* refactor: DispatcherServlet 위치 이동

* refactor: DispatcherServlet 위치 이동

* refactor: 인덱스 페이지 어노테이션 기반 출력

* refactor: 데이터 사이즈 1개일 때 값을 그대로 반환

---------

Co-authored-by: kang-hyungu <[email protected]>
  • Loading branch information
rawfishthelgh and kang-hyungu authored Sep 26, 2023
1 parent d64fa12 commit 896e811
Show file tree
Hide file tree
Showing 23 changed files with 104 additions and 123 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.DispatcherServlet;

/**
* Base class for {@link WebApplicationInitializer}
Expand Down
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 showPage(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(new JspView("/index.jsp"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Controller
public class LoginController {

@RequestMapping(value = "/login/view", method = RequestMethod.GET)
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView showPage(HttpServletRequest request, HttpServletResponse response) {
if (UserSession.isLoggedIn(request.getSession())) {
return new ModelAndView(new JspView("redirect:/index.jsp"));
Expand Down

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 showPage(HttpServletRequest request, HttpServletResponse response) {
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
Expand Up @@ -23,7 +23,7 @@ public ModelAndView register(HttpServletRequest request, HttpServletResponse res
return new ModelAndView(new JspView("redirect:/index.jsp"));
}

@RequestMapping(value = "/register/view", method = RequestMethod.GET)
@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView showPage(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(new JspView("/register.jsp"));
}
Expand Down

This file was deleted.

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

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;
}
}
2 changes: 1 addition & 1 deletion app/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<!-- before login -->
<div class="navbar-nav d-none d-md-inline-block ms-auto me-0 me-md-3 my-2 my-md-0">
<a class="nav-link" href="/login/view" role="button"><i class="fas fa-user fa-fw"></i>&nbsp;로그인</a>
<a class="nav-link" href="/login" role="button"><i class="fas fa-user fa-fw"></i>&nbsp;로그인</a>
</div>
<!-- before login -->

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</div>
<% } else { %>
<div class="navbar-nav d-none d-md-inline-block ms-auto me-0 me-md-3 my-2 my-md-0">
<a class="nav-link" href="/login/view" role="button"><i class="fas fa-user fa-fw"></i>&nbsp;로그인</a>
<a class="nav-link" href="/login" role="button"><i class="fas fa-user fa-fw"></i>&nbsp;로그인</a>
</div>
<% } %>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/webapp/login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</form>
</div>
<div class="card-footer text-center py-3">
<div class="small"><a href="/register/view">아이디가 없나요? 회원가입 하러가기</a></div>
<div class="small"><a href="/register">아이디가 없나요? 회원가입 하러가기</a></div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerMapping;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerMapping;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.techcourse;
package webmvc.org.springframework.web.servlet.mvc;

import com.techcourse.handlerMapper.AnnotationHandlerAdapter;
import com.techcourse.handlerMapper.AnnotationHandlerMapping;
import com.techcourse.handlerMapper.HandlerAdapter;
import com.techcourse.handlerMapper.HandlerMapping;
import com.techcourse.handlerMapper.ManualHandlerAdapter;
import com.techcourse.handlerMapper.ManualHandlerMapping;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -14,6 +8,12 @@
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.handlerMapper.AnnotationHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.HandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.HandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerMapping;

import java.util.ArrayList;
import java.util.List;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.techcourse.handlerMapper;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper;

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 com.techcourse.handlerMapper;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper;

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 com.techcourse.handlerMapper;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper;

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 com.techcourse.handlerMapper;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper;

import jakarta.servlet.http.HttpServletRequest;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.techcourse.handlerMapper;
package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper;

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

import com.techcourse.controller.LogoutController;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -18,9 +17,6 @@ public class ManualHandlerMapping implements HandlerMapping {

@Override
public void initialize() {
controllers.put("/", new ForwardController("/index.jsp"));
controllers.put("/logout", new LogoutController());

log.info("Initialized Handler Mapping!");
controllers.keySet()
.forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package webmvc.org.springframework.web.servlet.view;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import web.org.springframework.http.MediaType;
import webmvc.org.springframework.web.servlet.View;

import java.io.PrintWriter;
import java.util.Map;

public class JsonView implements View {

@Override
public void render(final Map<String, ?> model, final HttpServletRequest request, HttpServletResponse response) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = extractJsonString(model, objectMapper);
PrintWriter printWriter = response.getWriter();
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
printWriter.write(jsonString);
}

private String extractJsonString(Map<String, ?> model, ObjectMapper objectMapper) throws JsonProcessingException {
if (model.size() == 1) {
Object value = model.values().toArray()[0];
return objectMapper.writeValueAsString(value);
}
return objectMapper.writeValueAsString(model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.HandlerMapping;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class AnnotationHandlerMappingTest {

private AnnotationHandlerMapping handlerMapping;
private HandlerMapping handlerMapping;

@BeforeEach
void setUp() {
Expand Down

0 comments on commit 896e811

Please sign in to comment.