springmvc(2)--参数绑定
public class RestInDto implements Serializable {
private static final long serialVersionUID = -5461373449802431627L;
private String userName;
private BigDecimal salary;
private boolean isVip;
private int id;
......
}
@RequestMapping(value = "/getdatabinder", method = RequestMethod.GET)
public RestInDto testDataBinder(RestInDto restInDto){
Map<String,Object> m=new HashMap<String,Object>(); return restInDto;
}
2.基本复合对象类型
public class CusDto {
private String userName;
...
}
public class RestInDto implements Serializable {
private static final long serialVersionUID = -5461373449802431627L;
private String userName;
private BigDecimal salary;
private boolean isVip;
private int id;
private CusDto anotherDto;
...
}
url:http://127.0.0.1:8080/springtry/rest/getdatabinder?userName=中国龙&salary=12&id=0&vip=false&anotherDto.userName=中国风&vip=false
@RequestMapping(value = "/getdatabinder", method = RequestMethod.GET)
public RestInDto testDataBinder(RestInDto restInDto){
Map<String,Object> m=new HashMap<String,Object>(); return restInDto;
}
3.基本类型数组
标红部分相同就可以了
url:http://127.0.0.1:8080/springtry/rest/getdatabinderarr?restInDto=1&restInDto=2
@RequestMapping(value = "/getdatabinderarr", method = RequestMethod.GET)
public String[] testDataBinder(String[] restInDto){
Map<String,Object> m=new HashMap<String,Object>(); return restInDto;
}
另外:如果是ajax的话ajax,客户端 data:{restInDto:[]},服务端:
public String[] testDataBinder(@RequestParam(value="restInDto[]")String[] restInDto)
4.基本类型或由基本类型构成的自定义对象list
不能在controller方法中直接使用List<RestInDto>,而应该将List<RestInDto>作为另一个类的字段使用
public class RestInListDto {
private List<RestInDto> restIns;
...
}
url:http://127.0.0.1:8080/springtry/rest/getdatabinderlist?restIns[0].userName=中国&restIns[0].salary=12&restIns[0].id=0&restIns[0].vip=false
@RequestMapping(value = "/getdatabinderlist", method = RequestMethod.GET)
public RestInListDto testDataBinder(RestInListDto restInListDto){
Map<String,Object> m=new HashMap<String,Object>(); return restInListDto;
}
5.基本类型或由基本类型构成的自定义对象set
与list类似,只是要定义构造函数并在其中构建set
public class RestInSetDto {
private Set<RestInDto> restIns=new HashSet<RestInDto>();
public RestInSetDto() {
this.restIns.add(new RestInDto());
}
...
}
url:http://127.0.0.1:8080/springtry/rest/getdatabinderset?restIns[0].userName=中国&restIns[0].salary=12&restIns[0].id=0&restIns[0].vip=false
@RequestMapping(value = "/getdatabinderset", method = RequestMethod.GET)
public RestInSetDto testDataBinder(RestInSetDto restInSetDto){
Map<String,Object> m=new HashMap<String,Object>(); return restInListDto;
}
ps:set有个限制,下标不能大于size()值
6.Map
public class RestInMapDto {
private Map<String,RestInDto> restIns;
...
}
url:http://127.0.0.1:8080/springtry/rest/getdatabindermap?
restIns['x'].userName=中国龙&restIns['x'].salary=12&restIns['x'].id=0&restIns['x'].vip=false
@RequestMapping(value = "/getdatabindermap", method = RequestMethod.GET)
public RestInMapDto testDataBinder(RestInMapDto restInMapDto){
Map<String,Object> m=new HashMap<String,Object>(); return restInMapDto;
}
二、再来看看参数绑定常用的注解
根据它们处理的Request的不同内容部分分为四类:
- 处理request uri 部分(这里指uri template中variable,不含queryString部分)的注解:@PathVariable;
- 处理request header部分的注解: @RequestHeader, @CookieValue;
- 处理request body部分的注解:@RequestParam, @RequestBody;
- 处理attribute类型的注解: @SessionAttributes, @ModelAttribute;
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController { @RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}
上面代码把URI template 中变量 ownerId的值和petId的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称
2.@RequestHeader、@CookieValue
Host localhost:8080
Accept text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
//...
}
上面的代码,把request header部分的 Accept-Encoding的值,绑定到参数encoding上了, Keep-Alive header的值绑定到参数keepAlive上
@CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上
3.@RequestParam, @RequestBody
- 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;
- 用来处理Content-Type: 为
application/x-www-form-urlencoded编码的内容,提交方式GET、POST; - 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定
- post方式,解常用来处理Content-Type: 不是
application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
因为配置有FormHttpMessageConverter,所以也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String, String>里,这种情况在某些特殊需求下使用;
不能处理multipart/form-data
- put方式,只能处理application/x-www-form-urlencoded或application/json, application/xml等,不能处理multipart/form-data
@SessionAttributes:
该注解用来绑定HttpSession中的attribute对象的值,便于在方法中的参数里使用。
该注解有value、types两个属性,可以通过名字和类型指定要使用的attribute 对象
@ModelAttribute
该注解有两个用法,一个是用于方法上,一个是用于参数上;
用于方法上时: 通常用来在处理@RequestMapping方法之前,为请求request对象绑定需要从后台查询的model;
用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上,要绑定的值来源于:
- @SessionAttributes 启用的attribute 对象上;
- @ModelAttribute 用于方法上时指定的model对象;
- 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中5.@ResponseBody
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
若要绑定的是简单类型: 调用@RequestParam来处理
若要绑定的是复杂对象类型: 调用@ModelAttribute来处理
springmvc(2)--参数绑定的更多相关文章
- (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...
- SpringMVC学习--参数绑定
spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收 ...
- SpringMVC(三) —— 参数绑定和数据回显
参数绑定的过程:就是页面向后台传递参数,后台接受的一个过程. 默认支持的参数类型:(就是你在方法上以形参的形式去定义一下的类型,就可以直接使用它) HttpServletRequest HttpSer ...
- springmvc(三) 参数绑定、
前面两章就介绍了什么是springmvc,springmvc的框架原理,并且会简单的使用springmvc以及ssm的整合,从这一章节来看,就开始讲解springmvc的各种功能实现,慢慢消化 --W ...
- SpringMVC【参数绑定、数据回显、文件上传】
前言 本文主要讲解的知识点如下: 参数绑定 数据回显 文件上传 参数绑定 我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定.. ...
- SpringMVC中参数绑定
SpringMVC中请求参数的接收主要有两种方式, 一种是基于HttpServletRequest对象获取, 另外一种是通过Controller中的形参获取 一 通过HttpServletReque ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
- SpringMVC高级参数绑定(数组和List)
本节内容: 参数绑定之数组 将表单的数据绑定到List 复制下上篇博客中的工程,作为今天开发的工程. 一.参数绑定之数组 1. 需求 在商品列表页面选中多个商品,然后删除. 2. 需求分析 功能要求商 ...
- 什么是springMvc的参数绑定?
参数绑定通俗来讲就是从页面传过来的数据通过SpringMvc进行接收.接收的数据类型可以有: (1)SpringMvc默认支持的类型:request.session.application等. (2) ...
随机推荐
- Lotus 迁移到Exchange 2010 之准备使用Transport 同步Lotus 相关信息!
这里我们先来分析下Lotus迁移到Exchange2010 的一些原理,由于存在一定周期的共存时间,因此在共存期间必须来实现相应的同步计划,整个同步计划包含了如下的同步计划:
- 如何判断js中的数据类型(转)
如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...
- CSS hack 和 IE浏览器条件判断 集中汇总
(从死了一次又一次终于挂掉的百度空间中抢救出来的,发表日期 2014-08-16) 未完待续 css hack ie 浏览器判断语句 360大多数网页的各浏览器兼容方法: <!DOCTYPE h ...
- 关于mysql函数GROUP_CONCAT
GROUP_CONCAT()是MySQL数据库提供的一个函数,通常跟GROUP BY一起用,具体可参考MySQL官方文挡:http://dev.mysql.com/doc/refman/5.0/en/ ...
- wikioi 3038 3n+1问题
题目描述 Description 3n+1问题是一个简单有趣而又没有解决的数学问题.这个问题是由L. Collatz在1937年提出的.克拉兹问题(Collatz problem)也被叫做hailst ...
- PP常见数据表
Table表 Short text短文本 AFFL Work order sequence加工单顺序 AFFT Order process instructions订单-流程指令 A ...
- python中import失败解决的简单办法
例如:import pkg_resources失败 可以print sys.path查看,从其他机器上cp -r过来即可,如下例子: 从另外一个正常的机器上scp过来/usr/ali/lib/pyth ...
- 如何利用PhoneGap制作地图APP
摘要:百度地图API是一套由javascript编写的地图程序接口,按说它应该运行在浏览器上.现在,只要利用PhoneGap,我们就能开发出移动平台上能使用的APP了! --------------- ...
- pylons使用多个数据库(multiple DB)
最近做的工程要修改成两个数据库的,一个测试数据库, 一个线上数据库. 所以就要把原来的只有一个数据库的改成两个数据库. 第一步:修改development.ini # SQLAlchemy datab ...
- HTML <base> 标签
定义和用法 <base> 标签为页面上的所有链接规定默认地址或默认目标. 通常情况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白. 使用 <base& ...