
- GET 방식의 쿼리문을 '이름 = 값' 형태 데이터로 전송하는 것
- 다중 쿼리문은 &로 구분
- 요청 파라미터 수가 많지 않을 때 사용
ex) http://localhost8080:/mycar?color=red&year=2025
요청 파라미터 이름 : color, year / 값 : red, 2025
@Controller
public class Example01Controller {
@GetMapping("/exam01")
public String requestMethod(@RequestParam("id") String userId, @RequestParam("passwd") String userPw, Model model) {
model.addAttribute("data1", "@RequestParam 예제");
model.addAttribute("data2", "요청 파라미터 id 값 : " + userId + "<br>요청 파라미터 passwd 값" + userPw);
return "viewPage";
}
}
@GetMapping("/exam02")
public String requestMethod(@RequestParam(value="id", defaultValue="관리자") String userId,
@RequestParam("passwd") String userPw, Model model) {
model.addAttribute("data1", "@RequestParam 예제");
model.addAttribute("data2", "요청 파라미터 id 값 : " + userId + "<br>요청 파라미터 passwd 값" + userPw);
return "viewPage";
}
URL의 요청 파라미터 값을 전달받을 때 사용
ex) http://localhost8080:/car/red
파라미터 이름 : color / 값 : red
@GetMapping("/exam03/{id}")
public String requestMethod(@PathVariable("id") String userId, Model model) {
model.addAttribute("data1", "@PathVariable 예제");
model.addAttribute("data2", "경로 변수 id 값: " + userId);
return "viewPage";
}
@GetMapping("/exam04/{id}/{passwd}")
public String requestMethod(@PathVariable("id") String userId, @PathVariable("passwd") String userPw, Model model) {
model.addAttribute("data1", "@PathVariable 예제");
model.addAttribute("data2", "경로 변수 id 값: " + userId +"<br> 경로 변수 passwd 값: " + userPw);
return "viewPage";
}
경로 변수와 동일하게 파라미터 값을 전달받으면서, 세미콜론으로 다중 데이터 전달받음.
; 로 변수 값을 모음
ex) http://localhost8080:/mycar:color=red;year=2025
매트릭스 변수 : color, year / 값 : red, 2025
@GetMapping("/exam05/{id}")
public String requestMethod(@PathVariable("id") String userId, @MatrixVariable("passwd") String userPw, Model model) {
model.addAttribute("data1", "@MatrixVariable 예제");
model.addAttribute("data2", "경로 변수 id 값: " + userId +"<br> 매트릭스 변수 passwd 값: " + userPw);
return "viewPage";
}
@GetMapping("/exam07/{id}")
public String requestMethod(
@PathVariable("id") String userId, @MatrixVariable(value="passwd", defaultValue="admin123") String userPw, Model model) {
model.addAttribute("data1", "@MatrixVariable 예제");
model.addAttribute("data2", "경로변수 id 값: " + userId + "<br> 매트릭스변수 passwd 값: " + userPw );
return "viewPage";
}
1. 요청 파라미터 수가 많지 않을 때 사용하는 요청 처리 메서드 애너테이션은?
2. URL의 요청 파라미터 값을 전달받을 때 사용하는 요청 처리 메서드 애너테이션은?
3. 세미콜론으로 다중 데이터 전달받는 요청 처리 메서드 애너테이션은?
4. 요청 파라미터는 ( ) 와 같은 형태로 전달된다.
5. ( )은 @MatrixVariable 속성 중에서 기본값으로 대체하여 사용하는 속성이다.
답:
1. @RequestParam
2. @PathVariable
3. @MatrixVariable
4. 이름=값
5. defaultValue
1. http://localhost8080:/movies/FindingNemo 를 입력했을 때 매핑이 되도록 코드를 작성하시오.
답: /movies/{title}, @PathVariable("title") String title
@GetMapping("_________________")
public String requestMovies01(_________________________, Model model) {
model.addAttribute("movieList", title);
return "movies";
}
2. http://localhost8080:/movies?title=FindingNemo 를 입력했을 때 매핑이 되도록 코드를 작성하시오.
답: /movies, @RequestParam("title") String title
@GetMapping("______")
public String requestMovies02(________________, Model model) {
model.addAttribute("movieList", title);
return "movies";
}
출처: 송미영,『 스프링 부트 완전 정복 』, 길벗(2024), p126-167.
Coner Spring1
Editor: soyee
| [Spring 1팀] 8장. 다국어 처리 (0) | 2025.11.21 |
|---|---|
| [Spring 1팀] 7장. 파일 업로드 처리 (0) | 2025.11.14 |
| [Spring 1팀] 6장. 폼 태그 (1) | 2025.11.07 |
| [Spring 1팀] 4장. 컨트롤러 구현 (0) | 2025.10.10 |
| [Spring 1팀] 1-3장. 스프링 부트 소개 & 개발 환경 설정 & 구조 (0) | 2025.10.03 |