-
Notifications
You must be signed in to change notification settings - Fork 305
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
[MVC 구현하기 - 3단계] 레오(차영호) 미션 제출합니다. #586
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5cf9631
feat: annotation 기반 핸들러 어댑터 작성
youngh0 8fd878d
feat: 핸들러 매핑 구현 및 DispatcherServlet 수정
youngh0 40a7baa
refactor: 메서드 추출
youngh0 1157954
feat: HandlerExecution 에서 instance 의존하도록 수정
youngh0 0efbad8
refactor: css 최신화
youngh0 931a189
refactor: 저장 타입 Object -> Controller 로 변경
youngh0 72b26f3
refactor: try-catch 예외 명시
youngh0 ed6aaee
refactor: 사용하지 않는 클래스 제거
youngh0 959b8ac
refactor: 메서드 순서 변경
youngh0 b2a5d6a
feat: view render 로직 JspView 로 분리
youngh0 6c41b79
feat: UserContrller 추가
youngh0 cd97b02
feat: JsonView 구현
youngh0 81c9199
feat: 레거시 컨트롤러 제거
youngh0 d1e93ed
feat: 사용하지 않는 ManualHandler코드 제거
youngh0 34b98ed
refactor: 패키지 변경
youngh0 316fc7c
feat: PLAIN_TEXT 미디어 타입 추가
youngh0 e18807c
Merge remote-tracking branch 'upstream/youngh0' into step3
youngh0 226d9e7
feat: try-with-resources 적용
youngh0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 0 additions & 22 deletions
22
app/src/main/java/com/techcourse/ManualHandlerAdapter.java
This file was deleted.
Oops, something went wrong.
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 execute(final HttpServletRequest request, final HttpServletResponse response) { | ||
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
22 changes: 0 additions & 22 deletions
22
app/src/main/java/com/techcourse/controller/LoginViewController.java
This file was deleted.
Oops, something went wrong.
15 changes: 10 additions & 5 deletions
15
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,15 +1,20 @@ | ||
package com.techcourse.controller; | ||
|
||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import webmvc.org.springframework.web.servlet.mvc.asis.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; | ||
|
||
public class LogoutController implements Controller { | ||
@Controller | ||
public class LogoutController { | ||
|
||
@Override | ||
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
@RequestMapping(value = "logout", method = RequestMethod.POST) | ||
public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
final var session = req.getSession(); | ||
session.removeAttribute(UserSession.SESSION_KEY); | ||
return "redirect:/"; | ||
return new ModelAndView(new JspView("redirect:/")); | ||
} | ||
} |
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; | ||
} | ||
} |
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
8 changes: 0 additions & 8 deletions
8
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.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
27 changes: 27 additions & 0 deletions
27
mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.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,15 +1,42 @@ | ||
package webmvc.org.springframework.web.servlet.view; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import web.org.springframework.http.MediaType; | ||
import webmvc.org.springframework.web.servlet.View; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.Map; | ||
|
||
public class JsonView implements View { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
public void render(final Map<String, ?> model, final HttpServletRequest request, HttpServletResponse response) throws Exception { | ||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); | ||
try (PrintWriter writer = response.getWriter()) { | ||
writeBody(model, response, writer); | ||
} | ||
|
||
} | ||
|
||
private void writeBody(Map<String, ?> model, HttpServletResponse response, PrintWriter writer) throws JsonProcessingException { | ||
if (model.size() == 1) { | ||
response.setContentType(MediaType.PLAIN_TEXT_UTF8_VALUE); | ||
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. 세심한 처리 👍 |
||
writeBodyByPlainText(model, writer); | ||
} else { | ||
String value = objectMapper.writeValueAsString(model); | ||
writer.write(value); | ||
} | ||
} | ||
|
||
private void writeBodyByPlainText(Map<String, ?> model, PrintWriter writer) { | ||
for (Map.Entry<String, ?> entry : model.entrySet()) { | ||
writer.write(entry.getValue().toString()); | ||
} | ||
} | ||
|
||
@Override | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
enum이 아니라 클래스로 뽑은 이유가 있나용??
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.
이 부분은 기존 코드가 class 로 되어 있어서 PLAIN_TEXT_UTF8_VALUE 상수만 추가한겁니다!
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.
아하 이해했습니다. 굿굿~~