Spring MVC @RequestParam

作者:範宗雲 来源:原创 发布时间:2015-03-22 归档:springmvc

开发环境 : JDK 7 Maven 3 Tomcat 7 Spring 4.1.5 Eclipse Luna
@RequestParam 注解用于绑定请求中的参数到处理方法的参数中。
示例代码片段 1
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/find", method = GET)
				public String find(Model model, @RequestParam int id) {
					model.addAttribute(MESSAGE, id);
					return RESULT_PAGE;
				}

				@RequestMapping(value = "/modify", method = GET)
				public String modify(Model model, @RequestParam Integer id) {
					model.addAttribute(MESSAGE, id == null ? "null" : id);
					return RESULT_PAGE;
				}
			
			}
			
@RequestParam 注解标注的参数默认是必须的 ( required = true ), 不可缺省。
就基本数据类型而言, 其值要求是必须的, 如果允许空值, 需要用包装类型来替代 ( required = true )。
GET /product/find?id=31100042 --> find --> 31100042
GET /product/find?id  --> 400 Error ( Bad Request )
GET /product/find?id= --> 400 Error ( Bad Request )
GET /product/modify?id --> modify --> null
GET /product/modify?id=31100042 --> modify --> 31100042
示例代码片段 2
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/search", method = GET)
				public String search(Model model, @RequestParam("name") String productName) {
					model.addAttribute(MESSAGE, productName);
					return RESULT_PAGE;
				}
			
			}
			
若处理方法的参数名与绑定的请求的参数名称不一致, 需要在处理方法中用 value 注解显式的指明绑定的请求的参数的名称。
GET /product/search?name=books&type=it --> search --> books
示例代码片段 3
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/add", method = GET)
				public String add(Model model, @RequestParam(required = false) int id) {
					model.addAttribute(MESSAGE, id);
					return RESULT_PAGE;
				}
				
				@RequestMapping(value = "/edit", method = GET)
				public String edit(Model model, @RequestParam(required = false) Integer id) {
					model.addAttribute(MESSAGE, id == null ? "null" : id);
					return RESULT_PAGE;
				}
			
			}
			
required :
用于表明请求参数是否是必须的。默认值为 true。
若为 required = false, 且请求没有提供参数, 则使用 null 作为参数的值, 这就要求参数类型不能是基本数据类型, 否则报 500
GET /product/add --> 500 Error
GET /product/add?id=31100042 --> add --> 31100042
GET /product/edit --> edit --> null
GET /product/product/edit?id=31100042 --> edit --> 31100042
示例代码片段 4
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/obtain", method = GET)
				public String obtain(Model model, @RequestParam(defaultValue = "desc") String sort) {
					model.addAttribute(MESSAGE, "sort by " + sort);
					return RESULT_PAGE;
				}
				
				@RequestMapping(value = "/select", method = GET)
				public String select(Model model, @RequestParam(defaultValue = "${default.amount}") int amount) {
					model.addAttribute(MESSAGE, "select " + amount + " records");
					return RESULT_PAGE;
				}
				
				@RequestMapping(value = "/fetch", method = GET)
				public String fetch(Model model, @RequestParam(defaultValue = "${default.food}") String food) {
					model.addAttribute(MESSAGE, "food is " + food + ", default is fruit");
					return RESULT_PAGE;
				}
			
			}
			
defaultValue :
当请求中没有提供参数或参数的值为空 ( "" ), 则使用 defaultValue 注解的值 ( 支持 SpEL )。相当于隐式的设置了 required = false。
product.properties
			default.amount = 10
			default.food = fruit
			
GET /product/obtain --> obtain --> desc
GET /product/obtain?sort --> obtain --> desc
GET /product/obtain?sort=asc --> obtain --> asc
GET /product/select --> select --> 10
GET /product/select?amount=15 --> select --> 15
GET /product/fetch --> fetch --> fruit
GET /product/fetch?food=vegetable --> fetch --> vegetable
示例代码片段 5
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/query", method = GET)
				public String query(Model model, @RequestParam Map<String, Object> params) {
					model.addAttribute(MESSAGE, params);
					return RESULT_PAGE;
				}
				
				@RequestMapping(value = "/seek", method = GET)
				public String seek(Model model, @RequestParam MultiValueMap<String, Object> params) {
					model.addAttribute(MESSAGE, params);
					return RESULT_PAGE;
				}
			
			}
			
当 @RequestParam 注解标注在 Map 或 MultiValueMap 类型参数上时 ( 参数名任意 ), 该参数包含所有请求的参数。
GET /product/query?type=it&name=java --> query --> {type=it, name=java}
GET /product/seek?province=guangdong&county=maoming --> seek --> {province=[guangdong], county=[maoming]}
示例代码片段 6
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/delete", method = GET)
				public String delete(Model model, @RequestParam int[] ids) {
					model.addAttribute(MESSAGE, "delete where id in " + Arrays.toString(ids));
					return RESULT_PAGE;
				}
				
				@RequestMapping(value = "/remove", method = GET)
				public String remove(Model model, @RequestParam("ids") int[] productIds) {
					model.addAttribute(MESSAGE, "remove where id in " + Arrays.toString(productIds));
					return RESULT_PAGE;
				}
				
				@RequestMapping(value = "/update", method = GET)
				public String update(Model model, @RequestParam List<Integer> ids) {
					model.addAttribute(MESSAGE, "update where id in " + ids);
					return RESULT_PAGE;
				}
			
			}
			
用数组或集合类型绑定请求参数, 请求参数的值用 "," 分隔即可。
GET /product/delete?ids=31100042,31100043 --> delete --> [31100042, 31100043]
GET /product/remove?ids=31100044,31100045 --> remove --> [31100044, 31100045]
GET /product/update?ids=31100046,31100047 --> update --> [31100046, 31100047]

示例代码下载
spring-mvc-request-param.zip