---
title: "Spring学习笔记之@RequestParam作用"
author: "PrisonBreak"
date: "2015-01-24T22:12:07Z"
tags: []
categories: ["编程语言"]
slug: "spring-requestparam"
draft: false
lastmod: "2015-01-24T22:21:30Z"
id: 2179336
url: "https://prisonbreak.iteye.com/blog/2179336"
---

### 使用`@RequestParam`绑定请求参数到方法参数

`@RequestParam`注解是用于在控制器中绑定请求参数到方法参数。

下面取自PetClinic实例程序的代码片段说明了这种用法：

```
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {

    // ...

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
		Pet pet = this.clinic.loadPet(petId);
		model.addAttribute("pet", pet);
		return "petForm";
	}

    // ...
```

 使用这个注解的参数默认是必需的，但是可以把`@RequestParam`的`required`属性置为`false`从而让这个参数可选（例如，`@RequestParam(value="id", required="false")`）。

 

引自Spring Framework开发文档