파라미터 연결 후 요청 처리하기

✒️ 2025-06-24 00:15 내용 수정


사용하는 Annotation

1. @RequestParam

public String test(
	@RequestParam(name="name", required=true, defaultValue="0") dataType name
)
옵션 설명
name 이름 가져올 파라미터의 이름
required true/false 파라미터 필수 여부. true라면 반드시 값이 넘어와야 하며, 값이 넘어오지 않으면 400 error 발생
defaultValue 기본값 넘어온 값이 없을 때 기본값 설정
@Controller
public class SearchController {  
    @GetMapping("/search")  
    public String search(@RequestParam String keyword, Model model) {  
        model.addAttribute("searchKeyword", keyword);  
        model.addAttribute("message", keyword+"에 대한 검색 결과입니다.");  
        return "searchResult";  
    }  
}
<!doctype html>  
<html lang="ko" xmlns:th="http://www.thymeleaf.org">  
<head>  
    <title>Search Result</title>  
</head>  
<body>  
    <h1 th:text="${message}"></h1>  
    <p th:text="${searchKeyword}"></p>  
</body>  
</html>

springboot_parameter 1.png


2. @PathVariable

@GetMapping("/users/{id}")
public String test(@PathVariable Long id) {}
// controller
@Controller  
public class UserController {  
    @GetMapping("/users/{id}/{postId}")  
    public String user(  
		@PathVariable String id,  
		@PathVariable Long postId,  
		Model model)  
    {  
        model.addAttribute("userId", id);  
        model.addAttribute("postId", postId);  
        model.addAttribute("message", id+"번 사용자 정보입니다.");  
        return "userInfo";  
    }  
}
<!doctype html>  
<html lang="ko" xmlns:th="http://www.thymeleaf.org">  
<head>  
    <title>User Info</title>  
</head>  
<body>  
    <h1 th:text="${message}"></h1>  
    <p th:text="'사용자 : ' + ${userId}"></p>  
    <p th:text="'글 : ' + ${postId}"></p>  
</body>  
</html>

springboot_parameter 2.png


3. @ModelAttribute

public String test(@ModelAttribute("dto") PersonDTO dto) {}
public @ModelAttribute("name") int test() {}
// controller
@Controller  
public class RegisterController {  
    @GetMapping("/register")  
    public String registerForm() {  
        return "userForm";  
    }  
  
    @PostMapping("/register")  
    public String registerUser(@ModelAttribute User user, Model model) {  
        model.addAttribute("user", user);  
        model.addAttribute("message", "성공적으로 가입되었습니다.");  
        return "registerSuccess";  
    }  
}
<!doctype html>  
<html lang="ko" xmlns:th="http://www.thymeleaf.org">  
<head>  
    <title>Register Form</title>  
</head>  
<body>  
    <form action="/register" method="post">  
        <div>이름 : <input type="text" name="name"></div>  
        <div>이메일 : <input type="email" name="email"></div>  
        <div>나이 : <input type="number" name="age"></div>  
        <div>phone : <input type="text" name="phone"></div>  
        <button type="submit">가입</button>  
    </form>  
</body>  
</html>

springboot_parameter 3.png

<!doctype html>  
<html lang="ko" xmlns:th="http://www.thymeleaf.org">  
<head>  
    <title>Register Success</title>  
</head>  
<body>  
    <h1 th:text="${message}"></h1>  
    <div th:object="${user}">  
        <p th:text="'이름 : ' + *{name}"></p>  
        <p th:text="'이메일 : ' + *{email}"></p>  
        <p th:text="'나이 : ' + *{age}"></p>  
        <p th:text="'전화번호 : ' + *{phone}"></p>  
    </div>  
    <a th:href="@{/register}">다시 가입하기</a>  
</body>  
</html>

springboot_parameter 4.png