-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MVC 구현하기 - 3단계] 채채 (신채원) 미션 제출합니다 (#613)
* refactor: 어노테이션 매핑만 남도록 컨트롤러 수정 * feat: ManualHandlerMapping 클래스 제거 * feat: user 컨트롤러 추가 * refactor: 어노테이션 컨트롤러 빼로 제거 * feat: jsonView 구현 * refactor: 기존 초기화 제거 * style: final 추가 및 개행 수정 * style: 통일되지 않은 네이밍 수정 * refactor: 레거시 코드 제거 * refactor: 패키지 이동 * refactor: 변수명 수정 * refactor: 컨벤션에 따라 수정
- Loading branch information
1 parent
0ef6e38
commit d0eb125
Showing
20 changed files
with
163 additions
and
182 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
40 changes: 0 additions & 40 deletions
40
app/src/main/java/com/techcourse/ManualHandlerMapping.java
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/techcourse/controller/IndexController.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.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 IndexController { | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView index(final HttpServletRequest req, | ||
final HttpServletResponse res) { | ||
return new ModelAndView(new JspView("index.jsp")); | ||
} | ||
} |
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
24 changes: 13 additions & 11 deletions
24
app/src/main/java/com/techcourse/controller/LoginViewController.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 |
---|---|---|
@@ -1,32 +1,34 @@ | ||
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 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.mvc.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class LoginViewController implements Controller { | ||
@Controller | ||
public class LoginViewController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) { | ||
return UserSession.getUserFrom(req.getSession()) | ||
@RequestMapping(value = "/login/view", method = RequestMethod.GET) | ||
public ModelAndView loginView(final HttpServletRequest request, | ||
final HttpServletResponse response) { | ||
final String path = execute(request, response); | ||
return new ModelAndView(new JspView(path)); | ||
} | ||
|
||
private String execute(final HttpServletRequest request, | ||
final HttpServletResponse response) { | ||
return UserSession.getUserFrom(request.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return "redirect:/index.jsp"; | ||
}) | ||
.orElse("/login.jsp"); | ||
} | ||
|
||
@RequestMapping(value = "/login/view", method = RequestMethod.GET) | ||
public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { | ||
String path = execute(req, res); | ||
return new ModelAndView(new JspView(path)); | ||
} | ||
} |
19 changes: 7 additions & 12 deletions
19
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 |
---|---|---|
@@ -1,25 +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.mvc.asis.Controller; | ||
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) { | ||
final var session = req.getSession(); | ||
@RequestMapping(value = "/logout", method = RequestMethod.GET) | ||
public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) { | ||
final var session = request.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return "redirect:/"; | ||
} | ||
|
||
@RequestMapping(value = "/logout", method = RequestMethod.POST) | ||
public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) { | ||
String path = execute(req, res); | ||
return new ModelAndView(new JspView(path)); | ||
return new ModelAndView(new JspView("redirect:/")); | ||
} | ||
} |
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
16 changes: 6 additions & 10 deletions
16
app/src/main/java/com/techcourse/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,19 @@ | ||
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.mvc.asis.Controller; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
public class RegisterViewController implements Controller { | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) { | ||
return "/register.jsp"; | ||
} | ||
@Controller | ||
public class RegisterViewController { | ||
|
||
@RequestMapping(value = "/register/view", method = RequestMethod.GET) | ||
public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { | ||
String path = execute(req, res); | ||
return new ModelAndView(new JspView(path)); | ||
public ModelAndView loginView(final HttpServletRequest req, | ||
final HttpServletResponse res) { | ||
return new ModelAndView(new JspView("/register.jsp")); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
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(final HttpServletRequest request, | ||
final 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; | ||
} | ||
} |
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.