Skip to content

Commit

Permalink
[MVC 구현하기 - 3단계] 밀리(김미성) 미션 제출합니다. (#630)
Browse files Browse the repository at this point in the history
* refactor: 메서드명 변경

* feat: JsonView 구현

* feat: ViewResolver 구현

* refactor: Legacy 코드 제거

* refactor: test 제거

* refactor: DispatcherServlet 패키지 이동

* refactor: 핸들러 매핑의 getHandler가 Optional을 반환하도록 수정

* refactor: 핸들러 매핑 실패 시 예외 처리 수정

* refactor: 뷰 리졸버 처리 실패 시 예외 처리 수정

* refactor: 패키지 구조 변경

* refactor: 일급컬렉션으로 개선

* refactor: 핸들러의 반환타입이 String인 경우도 명시적으로 나타내도록 수정

* refactor: 메서드 분리
  • Loading branch information
miseongk authored Sep 27, 2023
1 parent 806b56b commit 6e9532c
Show file tree
Hide file tree
Showing 38 changed files with 420 additions and 353 deletions.
89 changes: 0 additions & 89 deletions app/src/main/java/com/techcourse/DispatcherServlet.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
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}
* implementations that register a {@link DispatcherServlet} in the servlet context.
* Base class for {@link WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in the
* servlet context.
*/
public class DispatcherServletInitializer implements WebApplicationInitializer {

Expand Down
38 changes: 0 additions & 38 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

19 changes: 0 additions & 19 deletions app/src/main/java/com/techcourse/WrappedManualHandlerMapping.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;

@Controller
public class ForwardController {

private static final String PATH = "/index.jsp";

@RequestMapping(value = "/", method = RequestMethod.GET)
public String execute(final HttpServletRequest request, final HttpServletResponse response) {
return PATH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

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;

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

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

@Override
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
if (UserSession.isLoggedIn(req.getSession())) {
return "redirect:/index.jsp";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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;

public class LoginViewController implements Controller {
@Controller
public class LoginViewController {

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

@Override
@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
return UserSession.getUserFrom(req.getSession())
.map(user -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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;

public class LogoutController implements Controller {
@Controller
public class LogoutController {

@Override
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
final var session = req.getSession();
session.removeAttribute(UserSession.SESSION_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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;

public class RegisterController implements Controller {
@Controller
public class RegisterController {

@Override
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
final var user = new User(2,
req.getParameter("account"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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;

public class RegisterViewController implements Controller {
@Controller
public class RegisterViewController {

@Override
@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
return "/register.jsp";
}
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(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 @@ -6,10 +6,10 @@

public class ModelAndView {

private final View view;
private final Object view;
private final Map<String, Object> model;

public ModelAndView(final View view) {
public ModelAndView(final Object view) {
this.view = view;
this.model = new HashMap<>();
}
Expand All @@ -27,7 +27,7 @@ public Map<String, Object> getModel() {
return Collections.unmodifiableMap(model);
}

public View getView() {
public Object getView() {
return view;
}
}
Loading

0 comments on commit 6e9532c

Please sign in to comment.