Skip to content

Commit

Permalink
[MVC 구현 - 3단계] 스플릿-박상현 미션 제출합니다. (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
splitCoding authored Sep 27, 2023
1 parent 25abc90 commit 3b8d02c
Show file tree
Hide file tree
Showing 34 changed files with 502 additions and 292 deletions.
63 changes: 0 additions & 63 deletions app/src/main/java/com/techcourse/DispatcherServlet.java

This file was deleted.

13 changes: 10 additions & 3 deletions app/src/main/java/com/techcourse/DispatcherServletInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.WebApplicationInitializer;
import webmvc.org.springframework.web.servlet.mvc.tobe.DispatcherServlet;
import webmvc.org.springframework.web.servlet.mvc.tobe.handleradapter.AnnotationHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.handlermapping.AnnotationHandlerMapping;

/**
* 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 All @@ -17,12 +20,16 @@ public class DispatcherServletInitializer implements WebApplicationInitializer {

@Override
public void onStartup(final ServletContext servletContext) {
final String packageName = getClass().getPackageName();
final var dispatcherServlet = new DispatcherServlet();

dispatcherServlet.addHandlerMapping(new AnnotationHandlerMapping(packageName));
dispatcherServlet.addHandlerAdapter(new AnnotationHandlerAdapter());
dispatcherServlet.addExceptionHandlerMapping(new NotFoundResponseHandler());
final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet);
if (registration == null) {
throw new IllegalStateException("Failed to register servlet with name '" + DEFAULT_SERVLET_NAME + "'. " +
"Check if there is another servlet registered under the same name.");
"Check if there is another servlet registered under the same name.");
}

registration.setLoadOnStartup(1);
Expand Down
47 changes: 0 additions & 47 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

33 changes: 33 additions & 0 deletions app/src/main/java/com/techcourse/NotFoundResponseHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.techcourse;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Objects;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.tobe.exceptionhandlermapping.ExceptionHandler;
import webmvc.org.springframework.web.servlet.view.JspView;

public class NotFoundResponseHandler implements ExceptionHandler {

private static final int NOT_FOUND_STATUS_CODE = 404;

private String viewName;

@Override
public void initialize() {
this.viewName = "404.jsp";
}

@Override
public boolean isHandleable(final HttpServletRequest request, final HttpServletResponse response) {
return response.getStatus() == NOT_FOUND_STATUS_CODE;
}

@Override
public ModelAndView handle(final HttpServletRequest request) {
if (Objects.nonNull(viewName)) {
return new ModelAndView(new JspView(viewName));
}
throw new UnsupportedOperationException();
}
}
20 changes: 20 additions & 0 deletions app/src/main/java/com/techcourse/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +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.view.JspView;

@Controller
public class HomeController {

private static final JspView INDEX_PAGE_VIEW = new JspView("/index.jsp");

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(final HttpServletRequest request, final HttpServletResponse response) {
return new ModelAndView(INDEX_PAGE_VIEW);
}
}
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,36 +2,42 @@

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;
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);
private static final View REDIRECT_UNAUTHORIZED_VIEW = new JspView("redirect:/401.jsp");
private static final JspView REDIRECT_INDEX_PAGE_VIEW = new JspView("redirect:/index.jsp");

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(final HttpServletRequest req, final HttpServletResponse res) {
if (UserSession.isLoggedIn(req.getSession())) {
return "redirect:/index.jsp";
return new ModelAndView(REDIRECT_INDEX_PAGE_VIEW);
}

return InMemoryUserRepository.findByAccount(req.getParameter("account"))
.map(user -> {
log.info("User : {}", user);
return login(req, user);
})
.orElse("redirect:/401.jsp");
.map(user -> checkUser(req, user))
.orElse(new ModelAndView(REDIRECT_UNAUTHORIZED_VIEW));
}

private String login(final HttpServletRequest request, final User user) {
private ModelAndView checkUser(final HttpServletRequest request, final User user) {
log.info("User : {}", user);

if (user.checkPassword(request.getParameter("password"))) {
final var session = request.getSession();
session.setAttribute(UserSession.SESSION_KEY, user);
return "redirect:/index.jsp";
return new ModelAndView(REDIRECT_INDEX_PAGE_VIEW);
}
return "redirect:/401.jsp";
return new ModelAndView(REDIRECT_UNAUTHORIZED_VIEW);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
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;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.View;
import webmvc.org.springframework.web.servlet.view.JspView;

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

private static final View LOGIN_PAGE_VIEW = new JspView("redirect:/login.jsp");
private static final JspView REDIRECT_INDEX_PAGE_VIEW = new JspView("redirect:/index.jsp");

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

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView showLoginPage(final HttpServletRequest req, final HttpServletResponse res) {
return UserSession.getUserFrom(req.getSession())
.map(user -> {
log.info("logged in {}", user.getAccount());
return "redirect:/index.jsp";
})
.orElse("/login.jsp");
.map(user -> {
log.info("logged in {}", user.getAccount());
return new ModelAndView(REDIRECT_INDEX_PAGE_VIEW);
})
.orElse(new ModelAndView(LOGIN_PAGE_VIEW));
}
}
17 changes: 12 additions & 5 deletions app/src/main/java/com/techcourse/controller/LogoutController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
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 {
private static final JspView REDIRECT_HOME_PAGE_VIEW = new JspView("redirect:/");

@RequestMapping(value = "/logout", method = RequestMethod.POST)
public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) {
final var session = req.getSession();
session.removeAttribute(UserSession.SESSION_KEY);
return "redirect:/";
return new ModelAndView(REDIRECT_HOME_PAGE_VIEW);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
@Controller
public class RegisterController {

@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) {
final String redirectViewName = "redirect:register.jsp";

return new ModelAndView(new JspView(redirectViewName));
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerUser(final HttpServletRequest req, final HttpServletResponse res) {
final String redirectViewName = "redirect:/index.jsp";
Expand All @@ -30,9 +23,9 @@ public ModelAndView registerUser(final HttpServletRequest req, final HttpServlet

private void saveUser(final HttpServletRequest request) {
final var user = new User(
request.getParameter("account"),
request.getParameter("password"),
request.getParameter("email")
request.getParameter("account"),
request.getParameter("password"),
request.getParameter("email")
);
InMemoryUserRepository.save(user);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +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 RegisterViewController implements Controller {
@Controller
public class RegisterViewController {

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
return "/register.jsp";
private static final JspView REGISTER_PAGE_VIEW = new JspView("/register.jsp");

@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public ModelAndView showRegisterPage(final HttpServletRequest req, final HttpServletResponse res) {
return new ModelAndView(REGISTER_PAGE_VIEW);
}
}
Loading

0 comments on commit 3b8d02c

Please sign in to comment.