1.作用:
@RequestParam:将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)
2.实战:
@Controller
@RequestMapping("test")
public class HelloController2 {
/**
* 接收普通请求参数
* http://localhost:8080/test/a?name=zhangsan
* url参数中的name必须要和@RequestParam("name")一致
* @return
*/
@RequestMapping("a")
public void test1(@RequestParam("name")String name){
sout(name);
//结果输出的是zhangsan
}
/**
* 接收普通请求参数
* http://localhost:8080/test/b
* url中没有name参数不会报错、有就显示出来
* @return
*/
@RequestMapping("b")
public ModelAndView test17(@RequestParam(value="name",required=false)String name){
sout(name);
//输出的是null
}
/**
* 接收普通请求参数
* http://localhost:8080/test/c?name=lisi显示为lisi
* http://localhost:8080/test/c?name 显示为hello
* @return
*/
@RequestMapping("c")
public ModelAndView test18(@RequestParam(value="name",required=true,defaultValue="hello")String name){
sout(name);
两种情况分别是显示为lisi和显示为hello
}
}