Skip to content

Commit

Permalink
[MVC 구현하기 - 3단계] 채채 (신채원) 미션 제출합니다 (#613)
Browse files Browse the repository at this point in the history
* refactor: 어노테이션 매핑만 남도록 컨트롤러 수정

* feat: ManualHandlerMapping 클래스 제거

* feat: user 컨트롤러 추가

* refactor: 어노테이션 컨트롤러 빼로 제거

* feat: jsonView 구현

* refactor: 기존 초기화 제거

* style: final 추가 및 개행 수정

* style: 통일되지 않은 네이밍 수정

* refactor: 레거시 코드 제거

* refactor: 패키지 이동

* refactor: 변수명 수정

* refactor: 컨벤션에 따라 수정
  • Loading branch information
chaewon121 authored Oct 10, 2023
1 parent 0ef6e38 commit d0eb125
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 182 deletions.
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;
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) {
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 {

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 {

@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 {

@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;
}
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

0 comments on commit d0eb125

Please sign in to comment.