Skip to content

Commit

Permalink
[MVC 구현하기 - 3단계] 블랙캣(송우석) 미션 제출합니다. (#591)
Browse files Browse the repository at this point in the history
* refactor: Jsp 렌더링을 JspView 객체로 위임

* refactor: 레거시 MVC 삭제 및 어노테이션 MVC로 변경

* feat: JSON 문자열을 객체로 직렬화 하는 기능 구현

* feat: 컨트롤러 메서드에서 세션, @RequestParam, @RequestBody 를 받을 수 있는 기능 구현

* feat: JSON 반환 기능 구현

* refactor: 컨트롤러 메서드 파라미터 변경

* refactor: app 모듈에서 mvc 프레임워크 모듈로 이동

* refactor: 코드 정렬

* refactor: DispatcherServletInitializer app 모듈로 이동

* refactor: 어노테이션 매핑 핸들러의 getHandler 반환타입 변경

* refactor: 로그인 뷰 - 로그인 요청 컨틀롤러 통합

* refactor: ObjectWriter, ObjectReader 로 static 선언

* remove: 사용하지 않는 메서드 제거
  • Loading branch information
Songusika authored Sep 26, 2023
1 parent 2ff83d8 commit c8974e1
Show file tree
Hide file tree
Showing 50 changed files with 723 additions and 281 deletions.
5 changes: 2 additions & 3 deletions app/src/main/java/com/techcourse/Application.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.techcourse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ public class DispatcherServletInitializer implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) {
final HandlerMappings handlerMappings = new HandlerMappings(List.of(
new ManualHandlerMapping(),
new AnnotationHandlerMapping("com.techcourse.controller")
));
final HandlerAdapters handlerAdapters = new HandlerAdapters(List.of(
new ManualHandlerAdapter(),
new AnnotationHandlerAdapter()
));
final var dispatcherServlet = new DispatcherServlet(handlerMappings, handlerAdapters);
Expand Down
26 changes: 0 additions & 26 deletions app/src/main/java/com/techcourse/ManualHandlerAdapter.java

This file was deleted.

42 changes: 0 additions & 42 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

3 changes: 1 addition & 2 deletions app/src/main/java/com/techcourse/TomcatStarter.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.techcourse;

import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.scan.StandardJarScanner;

import java.io.File;

public class TomcatStarter {

public static final String WEBAPP_DIR_LOCATION = "app/src/main/webapp/";
Expand Down
16 changes: 16 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,16 @@
package com.techcourse.controller;

import context.org.springframework.stereotype.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;

@Controller
public class IndexController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index() {
return new ModelAndView(new JspView("/index.jsp"));
}
}
48 changes: 34 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,33 +2,53 @@

import com.techcourse.domain.User;
import com.techcourse.repository.InMemoryUserRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import webmvc.org.springframework.web.servlet.mvc.asis.Controller;
import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpSession;
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 web.org.springframework.web.bind.annotation.RequestParam;
import webmvc.org.springframework.web.servlet.ModelAndView;
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) throws Exception {
if (UserSession.isLoggedIn(req.getSession())) {
return "redirect:/index.jsp";
@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView loginView(final HttpSession session) {
return UserSession.getUserFrom(session)
.map(user -> {
log.info("logged in {}", user.getAccount());
return "redirect:/index.jsp";
})
.map(it -> new ModelAndView(new JspView(it)))
.orElse(new ModelAndView(new JspView("/login.jsp")));
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(
final HttpSession session,
@RequestParam(value = "account") final String account,
@RequestParam("password") final String password
) {
if (UserSession.isLoggedIn(session)) {
return new ModelAndView(new JspView("redirect:/index.jsp"));
}

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

private String login(final HttpServletRequest request, final User user) {
if (user.checkPassword(request.getParameter("password"))) {
final var session = request.getSession();
private String login(final User user, final String password, final HttpSession session) {
if (user.checkPassword(password)) {
session.setAttribute(UserSession.SESSION_KEY, user);
return "redirect:/index.jsp";
}
Expand Down

This file was deleted.

19 changes: 11 additions & 8 deletions app/src/main/java/com/techcourse/controller/LogoutController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.techcourse.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import webmvc.org.springframework.web.servlet.mvc.asis.Controller;
import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpSession;
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 {
final var session = req.getSession();
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public ModelAndView logout(final HttpSession session) {
session.removeAttribute(UserSession.SESSION_KEY);
return "redirect:/";
return new ModelAndView(new JspView("redirect:/"));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.techcourse.controller;

import com.techcourse.domain.User;
import com.techcourse.dto.RegisterRequest;
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.RequestBody;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.ModelAndView;
Expand All @@ -14,18 +14,18 @@
public class RegisterController {

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

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

@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) {
@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public ModelAndView show() {
return new ModelAndView(new JspView("/register.jsp"));
}
}
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.dto.UserRequest;
import com.techcourse.repository.InMemoryUserRepository;
import context.org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.bind.annotation.RequestBody;
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(@RequestBody final UserRequest request) {
final String account = request.getAccount();
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;
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/techcourse/controller/UserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.techcourse.domain.User;
import jakarta.servlet.http.HttpSession;

import java.util.Optional;

public class UserSession {
Expand All @@ -18,5 +17,6 @@ public static boolean isLoggedIn(final HttpSession session) {
return getUserFrom(session).isPresent();
}

private UserSession() {}
private UserSession() {
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/techcourse/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ public boolean checkPassword(String password) {
return this.password.equals(password);
}

public long getId() {
return id;
}

public String getAccount() {
return account;
}

public String getPassword() {
return password;
}

public String getEmail() {
return email;
}

@Override
public String toString() {
return "User{" +
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/com/techcourse/dto/RegisterRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.techcourse.dto;

public class RegisterRequest {

private String account;
private String password;
private String email;

public RegisterRequest() {
}

public RegisterRequest(final String account, final String password, final String email) {
this.account = account;
this.password = password;
this.email = email;
}

public String getAccount() {
return account;
}

public String getPassword() {
return password;
}

public String getEmail() {
return email;
}
}
Loading

0 comments on commit c8974e1

Please sign in to comment.