您的位置:首页 > 编程语言 > Java开发

springMVC请求参数绑定

2015-07-26 11:28 302 查看
从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上。

springmvc中,接收页面提交的数据是通过方法形参来接收。而不是在controller类定义成员变更接收!!!!

1.1 默认支持的类型

直接在controller方法形参上定义下边类型的对象,就可以使用这些对象。在参数绑定过程中,如果遇到下边类型直接进行绑定。

1
HttpServletRequest

通过request对象获取请求信息

2 HttpServletResponse

通过response处理响应信息

3
HttpSession

通过session对象得到session中存放的对象

4Model/ModelMap

model是一个接口,modelMap是一个接口实现。

作用:将model数据填充到request域。

1.2 简单类型

通过@RequestParam对简单类型的参数进行绑定。

如果不使用@RequestParam,要求request传入参数名称和controller方法的形参名称一致,方可绑定成功。

如果使用@RequestParam,不用限制request传入参数名称和controller方法的形参名称一致。

@RequestMapping(value = "/hello",method = {RequestMethod.GET,RequestMethod.POST})
public String getInfos(int pin){
//
order.ListOrder();
System.out.println("from client inpyt"+pin);
return "a";
}

请求路径:http://localhost:8080/hello?pin=9,在接收参数中使用注解@requestparam(value="pin") int pins

在代码中就可以使用pins而不是pin

@RequestMapping(value = "/hello",method = {RequestMethod.GET,RequestMethod.POST})
public String getInfos(@RequestParam("pin") int pins){
//
order.ListOrder();
System.out.println("from client inpyt"+pins);
return "a";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: