-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MVC 구현 3단계] 오리(오현서) 미션 제출합니다. (#543)
* feat: JsonView 구현 * refactor: Login 로직 annotation Controller로 동작하도록 변경 * refactor: logout 로직 annotation Controller로 동작하도록 변경 * refactor: ForwardController 어노테이션 기반으로 변경 * refactor: 사용하지 않게된 ManulHandler 관련된 클래스 제거 * chore: DispatcherServlet 패키지 변경 * feat: UserController 추가 * refactor: JsonView가 요구사항에 맞게 view를 반환하도록 변경 * style: 개행 수정 * refactor: 개행 HandlerMapping 구현체 변경 * refactor: DispatcherServlet에서 JspView 의존성 제거
- Loading branch information
Showing
26 changed files
with
234 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
app/src/main/java/com/techcourse/ManualHandlerMapping.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/techcourse/controller/ForwardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
@Controller | ||
public class ForwardController { | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) { | ||
return new ModelAndView(new JspView("/index.jsp")); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/techcourse/controller/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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 Logger log = LoggerFactory.getLogger(LoginController.class); | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.POST) | ||
public ModelAndView login(final HttpServletRequest req, final HttpServletResponse res) { | ||
if (UserSession.isLoggedIn(req.getSession())) { | ||
return new ModelAndView(new JspView("redirect:/index.jsp")); | ||
} | ||
|
||
String responseUrl = InMemoryUserRepository.findByAccount(req.getParameter("account")) | ||
.map(user -> { | ||
log.info("User : {}", user); | ||
return login(req, user); | ||
}) | ||
.orElse("redirect:/401.jsp"); | ||
return new ModelAndView(new JspView(responseUrl)); | ||
} | ||
|
||
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); | ||
return "redirect:/index.jsp"; | ||
} | ||
return "redirect:/401.jsp"; | ||
} | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.GET) | ||
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { | ||
String responseUrl = UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return "redirect:/index.jsp"; | ||
}) | ||
.orElse("/login.jsp"); | ||
return new ModelAndView(new JspView(responseUrl)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/techcourse/controller/LogoutController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 LogoutController { | ||
|
||
@RequestMapping(value = "/logout", method = RequestMethod.GET) | ||
public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) { | ||
final var session = req.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return new ModelAndView(new JspView("redirect:/")); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...roller/annotation/RegisterController.java → ...course/controller/RegisterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/techcourse/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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) { | ||
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; | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
app/src/main/java/com/techcourse/controller/manual/LoginController.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
app/src/main/java/com/techcourse/controller/manual/LoginViewController.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
app/src/main/java/com/techcourse/controller/manual/LogoutController.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/techcourse/exception/NotFoundExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.techcourse.exception; | ||
|
||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.exception.HandlerNotFoundException; | ||
import webmvc.org.springframework.web.servlet.mvc.tobe.handler.ExceptionHandler; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class NotFoundExceptionHandler implements ExceptionHandler { | ||
|
||
@Override | ||
public boolean support(Throwable ex) { | ||
return ex instanceof HandlerNotFoundException; | ||
} | ||
|
||
@Override | ||
public ModelAndView handle() { | ||
return new ModelAndView(new JspView("/404.jsp")); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...techcourse/UncheckedServletException.java → .../exception/UncheckedServletException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.