Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MVC 구현하기 - 3단계] 가비 미션 제출합니다. #609

Merged
merged 9 commits into from
Sep 25, 2023
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
44 changes: 0 additions & 44 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

25 changes: 0 additions & 25 deletions app/src/main/java/com/techcourse/ManulHandlerAdapter.java

This file was deleted.

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

@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView showIndex(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(new JspView("index.jsp"));
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(new JspView("index.jsp"));
}

}
55 changes: 55 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,55 @@
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 {
private static final String REDIRECT_HOME = "redirect:/index.jsp";
private static final String REDIRECT_UNAUTHORIZED = "redirect:/401.jsp";

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

@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
return UserSession.getUserFrom(request.getSession())
.map(user -> {
log.info("logged in {}", user.getAccount());
return new ModelAndView(new JspView(REDIRECT_HOME));
})
.orElse(new ModelAndView(new JspView("/login.jsp")));
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
if (UserSession.isLoggedIn(request.getSession())) {
return new ModelAndView(new JspView(REDIRECT_HOME));
}

return InMemoryUserRepository.findByAccount(request.getParameter("account"))
.map(user -> {
log.info("User : {}", user);
return doLogin(request, user);
})
.orElse(new ModelAndView(new JspView(REDIRECT_UNAUTHORIZED)));
}

private ModelAndView doLogin(HttpServletRequest request, User user) {
if (user.checkPassword(request.getParameter("password"))) {
final var session = request.getSession();
session.setAttribute(UserSession.SESSION_KEY, user);
return new ModelAndView(new JspView(REDIRECT_HOME));
} else {
return new ModelAndView(new JspView(REDIRECT_UNAUTHORIZED));
}
}
}
21 changes: 21 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,21 @@
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 @controller 어노테이션이 누락된 것 같습니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정말 죄송합니다
마음이 급했습니다

@Controller
public class LogoutController {

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

}
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 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 RegisterController {

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView register(HttpServletRequest req, HttpServletResponse res) {
final var user = new User(2,
req.getParameter("account"),
req.getParameter("password"),
req.getParameter("email"));
InMemoryUserRepository.save(user);

return new ModelAndView(new JspView("redirect:/index.jsp"));
}

@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) {
return new ModelAndView(new JspView("/register.jsp"));
}

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

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
34 changes: 34 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,34 @@
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) {
String account = request.getParameter("account");
log.debug("user id : {}", account);

ModelAndView modelAndView = new ModelAndView(new JsonView());
User user = InMemoryUserRepository.findByAccount(account)
.orElseThrow();

modelAndView.addObject("user", user);
return modelAndView;
}

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

import com.techcourse.domain.User;
import jakarta.servlet.http.HttpSession;
Expand All @@ -9,6 +9,9 @@ public class UserSession {

public static final String SESSION_KEY = "user";

private UserSession() {
}

public static Optional<User> getUserFrom(final HttpSession session) {
final var user = (User) session.getAttribute(SESSION_KEY);
return Optional.ofNullable(user);
Expand All @@ -18,5 +21,4 @@ public static boolean isLoggedIn(final HttpSession session) {
return getUserFrom(session).isPresent();
}

private UserSession() {}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down
Loading