-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MVC 구현하기 - 3단계] 푸우(백승준) 미션 제출합니다. (#606)
* feat: UserController 추가 * feat: objectMapper 추가 및 사용법 테스트 * chore: 사용하지 않는 테스트 필드 삭제 * feat: JsonView 구현 * refactor: interface Controller 기반 Controller 를 annotation 기반으로 변경 * feat: ExceptionHandler 생성 * refactor: legacy interface Controller 삭제
- Loading branch information
Showing
33 changed files
with
338 additions
and
612 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
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
40 changes: 0 additions & 40 deletions
40
app/src/main/java/com/techcourse/ManualHandlerMapping.java
This file was deleted.
Oops, something went wrong.
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 forwardView(HttpServletRequest req, 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 { | ||
|
||
Logger log = LoggerFactory.getLogger(LoginController.class); | ||
|
||
@RequestMapping(value = "/login", method = {RequestMethod.GET}) | ||
public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { | ||
if (UserSession.isLoggedIn(req.getSession())) { | ||
return modelAndJspViewFor("redirect:index.jsp"); | ||
} | ||
return new ModelAndView(new JspView("/login.jsp")); | ||
} | ||
|
||
@RequestMapping(value = "/login", method = {RequestMethod.POST}) | ||
public ModelAndView handleLogin(HttpServletRequest req, HttpServletResponse res) { | ||
if (UserSession.isLoggedIn(req.getSession())) { | ||
return modelAndJspViewFor("redirect:index.jsp"); | ||
} | ||
|
||
return InMemoryUserRepository.findByAccount(req.getParameter("account")) | ||
.map(user -> { | ||
log.info("User : {}", user); | ||
return login(req, user); | ||
}) | ||
.orElseGet(() -> modelAndJspViewFor("redirect:/401.jsp")); | ||
} | ||
|
||
private ModelAndView 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 modelAndJspViewFor("redirect:/index.jsp"); | ||
} | ||
return modelAndJspViewFor("redirect:/401.jsp"); | ||
} | ||
|
||
private ModelAndView modelAndJspViewFor(String path) { | ||
return new ModelAndView(new JspView(path)); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
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,22 @@ | ||
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(HttpServletRequest req, 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
...troller/annotaion/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
2 changes: 1 addition & 1 deletion
2
...ler/annotaion/RegisterViewController.java → ...se/controller/RegisterViewController.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
32 changes: 32 additions & 0 deletions
32
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,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 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; | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
app/src/main/java/com/techcourse/controller/regacy/ForwardController.java
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
app/src/main/java/com/techcourse/controller/regacy/LoginController.java
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
app/src/main/java/com/techcourse/controller/regacy/LoginViewController.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
app/src/main/java/com/techcourse/controller/regacy/LogoutController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.