前面的文章介绍过注解@PathVariable,它可以为Rest风格的URL用占位符的方式传递一个参数,可是这个参数并非真正意义上的请求参数。请求参数怎么处理是本文的主要内容。html
Spring MVC 经过分析处理方法的签名,将 HTTP 请求信息绑定处处理方法的相应人参中。java
Spring MVC 对控制器处理方法签名的限制是很宽松的,几乎能够按喜欢的任何方式对方法进行签名。web
必要时能够对方法及方法入参标注相应的注解(@PathVariable、 @RequestParam、 @RequestHeader 等)、 SpringMVC 框架会将 HTTP 请求的信息绑定到相应的方法入参中,并根据方法的返回值类型作出相应的后续处理。spring
(本文出自:http://my.oschina.net/happyBKs/blog/417032)浏览器
在处理方法入参处使用 @RequestParam 能够把请求参数传递给请求方法mvc
– value:参数名app
– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常框架
控制器类和处理函数以下:jsp
package com.happyBKs.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @RequestMapping("class") @Controller public class RPTestHandler { String page="successrm"; @RequestMapping("student") public String handle(@RequestParam(value="username") String un, @RequestParam(value="age") int age) { System.out.println("a student's request has come. username: "+un+", age: "+age); return page; } }
这里使用@RequestParam注解的value属性值来映射请求参数。函数
请求页面index8.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <a href="class/student?username=happyBKs&age=100">class/student?username=happyBKs&age=100</a> </body> </html>
好,运行过程以下:
点击超拦截请求:
控制台此时输出:
a student's request has come. username: happyBKs, age: 100
也能够直接在浏览器地址栏输入http://localhost:8080/mymvc/class/student?username=happyBKs&age=101
控制台显示 a student's request has come. username: happyBKs, age: 101
问题1:若是咱们的请求少一个参数age,会怎么样?
这个问题该如何解决呢?
这里须要用到@RequestParam注解的required属性或defaultValue属性。
required属性标注这个参数是不是必需大的,默认是true,若是想让它能够不存在,那么就设置为false。可是请注意,required设置成false的参数对应的处理函数的参数类型必须是对象类型,不然回报错500!
例如这里咱们若是将控制器类改为:
package com.happyBKs.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @RequestMapping("class") @Controller public class RPTestHandler { String page="successrm"; @RequestMapping("student") public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false) int age) { System.out.println("a student's request has come. username: "+un+", age: "+age); return page; } }
结果会报错,由于age虽然设置了required为false,请求参数能够不包含age,可是在处理函数中对应的参数类型是int,int是基本数据类型,没法为空,因此报错。解决办法是将int改成Integer。
运行结果:
控制台输出:
a student's request has come. username: happyBKs, age: null
若是咱们仍然想用基本类型做为参数类型,那么能够用到@RequestParam注解的defaultValue属性来指定默认值。
package com.happyBKs.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @RequestMapping("class") @Controller public class RPTestHandler { String page="successrm"; @RequestMapping("student") public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false, defaultValue="0") int age) { System.out.println("a student's request has come. username: "+un+", age: "+age); return page; } }
运行结果:
控制台 a student's request has come. username: happyBKs, age: 0
最后,作个总结:(@RequestParam注解是很经常使用的,因此很重要)
* @RequestParam 来映射请求参数
* value值 即请求参数名
* required 该参数是否必需。默认为true
* defaultValue请求参数的默认值