@RequestParam
을 사용해 작성하면 항상 주소 마지막에 변수값이 들어가는 문제가 있다.@PathVariable
이라는 어노테이션을 사용하면 RESTful하게 파라미터 위치를 자유롭게 조정할 수 있었다.// HomeController
// 홈 전체조회
@GetMapping("/")
public String homeList(Model model) {
...
return "homes/homeList";
}
// 개별 홈 조회
@GetMapping("/homes")
public String selectHome (@RequestParam("homeId")Long homeId, Model model) {
...
return "homes/selectHome";
}
// UserController
// 회원가입 폼 조회
@GetMapping("/users/new")
public String createUserForm() {
return "users/createUserForm";
}
// 회원가입
@PostMapping("/users/new")
public String createUser(UserForm form, Model model) {
...
return "redirect:/";
}
// PostController
// 게시물 등록 폼 조회
@GetMapping("/posts/{homeId}/new")
public String createPostForm(@PathVariable("homeId")Long homeId, Model model) {
model.addAttribute("homeId", homeId);
return "posts/createPostForm";
}
// 게시물 등록
@PostMapping("/posts/{homeId}/new")
public String createPost(@PathVariable("homeId")Long homeId, PostForm form) {
...
return "redirect:/homes?homeId=" + homeId;
}
// 게시물 수정 폼 조회
@GetMapping("/posts/{homeId}/update/{postId}")
public String updatePostForm(@PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId, Model model) {
...
return "posts/updatePostForm";
}
// 게시물 수정
@PostMapping("/posts/{homeId}/update/{postId}")
public String updatePost(PostForm form, @PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId) {
...
return "redirect:/homes?homeId=" + form.getHomeId();
}
// 게시물 삭제
@PostMapping("/posts/{homeId}/delete/{postId}")
public String deletePost (@PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId) {
...
return "redirect:/homes?homeId=" + homeId;
}
Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException
<!-- 에러 발생 코드 -->
<td>
<a th:href="@{/posts/${owner.id}/update/${post.postId}}" th:text="수정"></a>
</td>
<!--수정 : @{}안 텍스트 양 옆에 | 추가. text와 변수를 함께 작성할 때 사용한다고 배웠던 기호인데 url에도 적용된다-->
<td>
<a th:href="@{|/posts/${owner.id}/update/${post.postId}|}" th:text="수정"></a>
</td>
WARN 4132 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]
<a href="" />
로 이동한 url은 get방식인데 controller의 delete는 post 방식 사용해서 일어나는 에러이다.thymeleaf
의 custom을 사용해 html 메소드를 바꾸는 방법도 있는 것 같았지만 당장 적용하기는 복잡해보였다.<!-- 에러 발생 코드 -->
<a th:href="@{|/posts/${owner.id}/delete/${post.postId}|}" custom:linkMethod="post" th:text="삭제"></a>
<!-- form으로 요청 전송 -->
<form method="POST" th:action="@{|/posts/${owner.id}/delete/${post.postId}|}">
<input type="hidden" name="postId" id="postId" th:value="${post.postId}" />
<button type="submit" name="submit">삭제</button>
</form>
// PostController.java
// 게시물 삭제
@PostMapping("/posts/{homeId}/delete/{postId}")
public String deletePost (@PathVariable("homeId")Long homeId, @PathVariable("postId")Long postId) {
postService.deletePost(postId);
return "redirect:/homes?homeId=" + homeId;
}
pull request
, merge
기능을 이용해 master 브랜치에 merge했다.