-
질문 수정 버튼을 만들었다면
그 버튼을 누를 때, @GetMapping으로 화면수정(재작성) URL을 가져오게 된다
이 때 return되는 것은, 질문 수정(재작성) 페이지인 "question_form"이다
-
질문 수정 페이지인 question_form으로 이동되고 나서,
수정 버튼(저장)을 누르면 @PostMapping이 작동하며
이떄 return되는 것은,
bindingResult가 에러일 때 question_form으로 재작성 요구
정상 작성 시, question/detail/%s, id 로 리디렉션 된다
-
주의해야 할 점은,
@GetMapping("/modify/{id}")의 URL은
다른 URL에서 이동되는 URL이고
@PostMapping("/modify/{id}")은
Post 처리되는 그 장소의 URL를 의미한다
-
@GetMapping | @PostMapping | |
URL(어노테이션) | @PreAuthorize("isAuthenticated()") @GetMapping("/modify/{id}") |
@PreAuthorize("isAuthenticated()") @PostMapping("/modify/{id}") |
메서드 | public String questionModify | public String questionModify |
인자 | QuestionForm questionForm - Principal principal @PathVariable("id") Integer id |
@Valid QuestionForm questionForm BindingResult bindingResult Principal principal @PathVariable("id") Integer id |
액션 | - | if (bindingResult.hasErrors()) return "question_form"; |
Question question = this.questionService.getQuestion(id); | Question question = this.questionService.getQuestion(id); | |
if (!question.getAuthor().getUsername().equals(principal.getName())) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "수정권한이 없습니다.");} |
if (!question.getAuthor().getUsername().equals(principal.getName())) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "수정권한이 없습니다.");} |
|
questionForm.setSubject(question.getSubject()); questionForm.setContent(question.getContent()); |
this.questionService.modify(question, questionForm.getSubject(), questionForm.getContent()); | |
리턴 | "question_form"; | String.format("redirect:/question/detail/%s", id); |
'포트폴리오 > SPRINGBOOT 옛날' 카테고리의 다른 글
3-12 : 앵커 (0) | 2022.10.13 |
---|---|
3-11 : 추천 (0) | 2022.10.13 |
3-10 수정과 삭제 (0) | 2022.10.10 |
3 - 09 : 글쓴이 표시 (0) | 2022.10.10 |
3-08 : entity에 author 추가하기 (0) | 2022.10.10 |