연습문제 풀이
URI 어노테이션 2가지 (경로 설정)
- @RequestParam
- URI 형식
- localhost:8080/update-form?index=1
- 메서드 구현
-
// 관리자 페이지 - 회원목록 상세 출력 // localhost:8080/update-form?index=1 @GetMapping("/update-form") public String viewUpdateForm(@RequestParam int index, Model model) { model.addAttribute("index", index); Member member = memberList.get(index-1); model.addAttribute("member", member); return "update-form"; }
-
- html에서 경로이동 설정
- th:href="@{/update-form(index=${status.count})}”
- URI 형식
- @PathVariable
- URI 형식
- localhost:8080/update-form/1
- 메서드 구현
-
// 관리자 페이지 - 회원목록 상세 출력 // localhost:8080/update-form/1 @GetMapping("/update-form/{index}") public String viewUpdateForm(@PathVariable int index, Model model) { model.addAttribute("index", index); Member member = memberList.get(index-1); model.addAttribute("member", member); return "update-form"; }
-
- html에서 경로이동 설정
- th:href="@{/update-form/{index}(index=${status.count})}"
- URI 형식
- 두 방법의 차이
- @PathVariable은 값을 하나만 받아올 수 있다.
- 여러 개의 데이터를 받아올 때는 @RequestParam을 사용한다.
- @RequestParam은 URI 외에도 ajax 요청을 통한 데이터를 받아올 수 있다. (Model 사용시 데이터 가져올 때)
- 보안이 필요한 회원가입 같은 작업을 할 때는 @RequestParam을 사용한다.
728x90
'교육 (Today I Learned) > Hanaro' 카테고리의 다른 글
[Hanaro] 59일차 / MyBatis (0) | 2024.04.10 |
---|---|
[Hanaro] 58일차 / Spring Boot (Thymeleaf Layout), JSP (0) | 2024.04.10 |
[Hanaro] 56일차 / MySQL 연동, Spring Boot (MVC, DTO) (0) | 2024.04.06 |
[Hanaro] 55일차 / Spring Boot (Model에서 REST API로 변환) (0) | 2024.04.06 |
[Hanaro] 54일차 / Spring Boot (Test) (0) | 2024.04.06 |