Web/Spring
[Annotation] @PathVariable
yj_oo_
2021. 8. 6. 11:41
"URL 경로에 변수를 넣어주는 것" "RESTful 서비스의 URI 형태"
사용법
@GetMapping({"/edit", "/edit/{id}"})
public String edit(@PathVariable(required = false) Integer id, Model model) {
위와 같이 GetMapping에서 id가 필요할 경우에만 사용할 땐 (required = false)를 붙인다.
그럼 id가 들어오면 /edit/{id}를 주고 id가 없으면 /edit을 준다.
@GetMapping("/read/{id}")
public String read(@PathVariable Integer id, Model model) {
위는 id가 필수적으로 필요하다.
❗ id가 없을 때도 잘 받기 위해선 view 쪽에 hidden input으로 id를 설정해주면 알아서 처리해줌... ❗
<form method="post" th:action="|@{/project/save}|" th:object="${project}">
<input th:field="*{id}" type="hidden"/>