-
Notifications
You must be signed in to change notification settings - Fork 304
[MVC 구현하기 - 2, 3단계] 포츈(정윤성) 미션 제출합니다. #97
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
Changes from 7 commits
2fa612d
db67d7b
98ca581
10580b1
3cafedb
50f304b
834708f
14eb85c
5faa6c5
a847513
53ead64
5ffc626
9a1dd20
e1c19b3
a4caf77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.techcourse.controller; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.mvc.view.JspView; | ||
import nextstep.mvc.view.ModelAndView; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.support.RequestMethod; | ||
|
||
@Controller | ||
public class ForwardController { | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView execute(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
return new ModelAndView(new JspView("/index.jsp")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,60 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserSession; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import nextstep.mvc.controller.asis.Controller; | ||
import nextstep.mvc.view.JspView; | ||
import nextstep.mvc.view.ModelAndView; | ||
import nextstep.mvc.view.View; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.support.RequestMethod; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class LoginController implements Controller { | ||
@Controller | ||
public class LoginController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LoginController.class); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class); | ||
|
||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse res) { | ||
@RequestMapping(value = "/login/view", method = RequestMethod.GET) | ||
public ModelAndView view(HttpServletRequest req, HttpServletResponse res) { | ||
return UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
LOGGER.info("logged in {}", user.getAccount()); | ||
View view = new JspView("redirect:/index.jsp"); | ||
return new ModelAndView(view); | ||
}) | ||
.orElse(new ModelAndView(new JspView("/login.jsp"))); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아래의 if (UserSession.isLoggedIn(req.getSession())) {
View view = new JspView("redirect:/index.jsp");
return new ModelAndView(view);
} 를 사용한다면, 가독성이 좋아지고 통일성이 생길 것 같아요! |
||
|
||
@RequestMapping(value = "/login", method = RequestMethod.POST) | ||
public ModelAndView login(HttpServletRequest req, HttpServletResponse res) { | ||
if (UserSession.isLoggedIn(req.getSession())) { | ||
return "redirect:/index.jsp"; | ||
View view = new JspView("redirect:/index.jsp"); | ||
return new ModelAndView(view); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return InMemoryUserRepository.findByAccount(req.getParameter("account")) | ||
.map(user -> { | ||
log.info("User : {}", user); | ||
return login(req, user); | ||
}) | ||
.orElse("redirect:/401.jsp"); | ||
.map(user -> { | ||
LOGGER.info("User : {}", user); | ||
return new ModelAndView(loginCheck(req, user)); | ||
}) | ||
.orElse(new ModelAndView( | ||
new JspView("redirect:/401.jsp") | ||
)); | ||
} | ||
|
||
private String login(HttpServletRequest request, User user) { | ||
private View loginCheck(HttpServletRequest request, User user) { | ||
if (user.checkPassword(request.getParameter("password"))) { | ||
final HttpSession session = request.getSession(); | ||
session.setAttribute(UserSession.SESSION_KEY, user); | ||
return "redirect:/index.jsp"; | ||
return new JspView("redirect:/index.jsp"); | ||
} else { | ||
return "redirect:/401.jsp"; | ||
return new JspView("redirect:/401.jsp"); | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.techcourse.domain.UserSession; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import nextstep.mvc.controller.asis.Controller; | ||
import nextstep.mvc.view.JspView; | ||
import nextstep.mvc.view.ModelAndView; | ||
import nextstep.mvc.view.View; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.support.RequestMethod; | ||
|
||
public class LogoutController implements Controller { | ||
@Controller | ||
public class LogoutController { | ||
|
||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
@RequestMapping(value = "/logout", method = RequestMethod.GET) | ||
public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
final HttpSession session = req.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return "redirect:/"; | ||
View view = new JspView("redirect:/"); | ||
return new ModelAndView(view); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.techcourse.controller; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.mvc.view.JsonView; | ||
import nextstep.mvc.view.ModelAndView; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.support.RequestMethod; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Controller | ||
public class MultiInputController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(MultiInputController.class); | ||
|
||
@RequestMapping(value = "/api/multi", method = RequestMethod.GET) | ||
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { | ||
String input1 = request.getParameter("input1"); | ||
String input2 = request.getParameter("input2"); | ||
|
||
LOGGER.debug("input1 : {}", input1); | ||
LOGGER.debug("input2 : {}", input2); | ||
|
||
ModelAndView modelAndView = new ModelAndView(new JsonView()); | ||
|
||
modelAndView.addObject("input1", input1); | ||
modelAndView.addObject("input2", input2); | ||
return modelAndView; | ||
} | ||
} | ||
Comment on lines
+13
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2개 입력에 대한 테스트가 용이하도록, 컨트롤러를 추가해주었어요. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,14 @@ | |
|
||
@Controller | ||
public class RegisterController { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이미 로그인이 된 사용자라면,
|
||
|
||
@RequestMapping(value = "/register", method = RequestMethod.POST) | ||
public ModelAndView save(final HttpServletRequest req, final HttpServletResponse res) { | ||
User user = new User(2, | ||
req.getParameter("account"), | ||
req.getParameter("password"), | ||
req.getParameter("email")); | ||
req.getParameter("email") | ||
); | ||
InMemoryUserRepository.save(user); | ||
|
||
return new ModelAndView(new JspView("redirect:/index.jsp")); | ||
|
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 jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.mvc.view.JsonView; | ||
import nextstep.mvc.view.ModelAndView; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.support.RequestMethod; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Controller | ||
public class UserController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class); | ||
|
||
@RequestMapping(value = "/api/user", method = RequestMethod.GET) | ||
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { | ||
final String account = request.getParameter("account"); | ||
LOGGER.debug("user id : {}", account); | ||
|
||
final ModelAndView modelAndView = new ModelAndView(new JsonView()); | ||
final User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(); | ||
|
||
modelAndView.addObject("user", user); | ||
return modelAndView; | ||
} | ||
} | ||
Comment on lines
+15
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final ModelAndView modelAndView = new ModelAndView(new JsonView());
modelAndView.addObject("user", user);
return modelAndView; 이렇게 final User user = InMemoryUserRepository.findByAccount(account)
.orElseThrow(); 의 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 작성한 코드가 아니고, 요구조건에서 주어진 class라서 그냥 곧이 그대로 가져왔었네요. ㅎㅎ;; 수정 하도록할게용 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package com.techcourse.controller; | ||
package com.techcourse.domain; | ||
|
||
import com.techcourse.domain.User; | ||
import jakarta.servlet.http.HttpSession; | ||
|
||
import java.util.Optional; | ||
|
||
public class UserSession { | ||
|
@@ -18,5 +16,6 @@ public static boolean isLoggedIn(HttpSession session) { | |
return getUserFrom(session).isPresent(); | ||
} | ||
|
||
private UserSession() {} | ||
private UserSession() { | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private 생성자의 접근제어자가 private 이다 보니까 생성자니까 멤버 변수 아래, 메서드 위에 두는게 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분은 자동 포매팅 때문에 종종 놓치게 되네요 ㅋㅋ; |
||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"/index.jsp"
등의 View name들이 여러 컨트롤러에 반복적으로 쓰이고 있어요.mvc 모듈의
nextstep.web.support
패키지의MediaType.class
처럼,별도의 클래스 상수용 클래스를 만들어 View name을 관리하면 어떨까요?