Skip to content

Commit

Permalink
[MVC 구현하기 - 3단계] 푸우(백승준) 미션 제출합니다. (#606)
Browse files Browse the repository at this point in the history
* feat: UserController 추가

* feat: objectMapper 추가 및 사용법 테스트

* chore: 사용하지 않는 테스트 필드 삭제

* feat: JsonView 구현

* refactor: interface Controller 기반 Controller 를 annotation 기반으로 변경

* feat: ExceptionHandler 생성

* refactor: legacy interface Controller 삭제
  • Loading branch information
BGuga authored Sep 25, 2023
1 parent e705560 commit 892d3ba
Show file tree
Hide file tree
Showing 33 changed files with 338 additions and 612 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
implementation "org.apache.tomcat.embed:tomcat-embed-jasper:10.1.13"
implementation "ch.qos.logback:logback-classic:1.2.12"
implementation "org.apache.commons:commons-lang3:3.13.0"
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2'

testImplementation "org.assertj:assertj-core:3.24.2"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.WebApplicationInitializer;
import webmvc.org.springframework.web.servlet.mvc.asis.AnnotationControllerAdapter;
import webmvc.org.springframework.web.servlet.mvc.asis.HandlerAdapterRegistry;
import webmvc.org.springframework.web.servlet.mvc.asis.InterfaceControllerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationControllerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.DispatcherServlet;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapterRegistry;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappingRegistry;

Expand Down Expand Up @@ -39,8 +39,6 @@ public void onStartup(final ServletContext servletContext) {

private HandlerMappingRegistry getHandlerMappingRegistry() {
HandlerMappingRegistry registry = new HandlerMappingRegistry();
registry.addHandlerMapping(
getInitializedHandlerMapping(new ManualHandlerMapping()));
registry.addHandlerMapping(
getInitializedHandlerMapping(new AnnotationHandlerMapping("com")));
return registry;
Expand All @@ -54,7 +52,6 @@ private HandlerMapping getInitializedHandlerMapping(HandlerMapping handlerMappin
private HandlerAdapterRegistry getHandlerAdapterRegistry() {
HandlerAdapterRegistry registry = new HandlerAdapterRegistry();
registry.addHandlerAdapter(new AnnotationControllerAdapter());
registry.addHandlerAdapter(new InterfaceControllerAdapter());
return registry;
}
}
40 changes: 0 additions & 40 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/ForwardController.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 ForwardController {

@RequestMapping(value = "/", method = {RequestMethod.GET})
public ModelAndView forwardView(HttpServletRequest req, HttpServletResponse res) {
return new ModelAndView(new JspView("/index.jsp"));
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/com/techcourse/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.JspView;

@Controller
public class LoginController {

Logger log = LoggerFactory.getLogger(LoginController.class);

@RequestMapping(value = "/login", method = {RequestMethod.GET})
public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) {
if (UserSession.isLoggedIn(req.getSession())) {
return modelAndJspViewFor("redirect:index.jsp");
}
return new ModelAndView(new JspView("/login.jsp"));
}

@RequestMapping(value = "/login", method = {RequestMethod.POST})
public ModelAndView handleLogin(HttpServletRequest req, HttpServletResponse res) {
if (UserSession.isLoggedIn(req.getSession())) {
return modelAndJspViewFor("redirect:index.jsp");
}

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

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 modelAndJspViewFor("redirect:/index.jsp");
}
return modelAndJspViewFor("redirect:/401.jsp");
}

private ModelAndView modelAndJspViewFor(String path) {
return new ModelAndView(new JspView(path));
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/techcourse/controller/LogoutController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 LogoutController {


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

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.techcourse.controller.annotaion;
package com.techcourse.controller;

import com.techcourse.domain.User;
import com.techcourse.repository.InMemoryUserRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.techcourse.controller.annotaion;
package com.techcourse.controller;

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
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;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 892d3ba

Please sign in to comment.