项目中用到ajax进行页面数据列表的刷新,调用时需要用到很多查询条件,以往的做法是用@RequestParam来逐个获取传入的参数,但参数多了,@RequestParam的个数会越来越多,查了下资料可以用@RequestBody来接收jason格式的参数列表并自动转化为自定义对象,但需要引入jackson
<dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>0.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-lgpl</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-lgpl</artifactId> <version>1.8.5</version> </dependency>
function getPageSize() { $.ajax({ type: "post", async: false, url: 'getPageSize', data: JSON.stringify({ sourceId: $("#source").val(), seriesId: $("#series").val(), modelId: $("#model").val(), city : $("#city").val(), strFrom: $("#from").val(), strTo: $("#to").val() }), dataType: "json", contentType: "application/json; charset=utf-8", success: function(data) { ... }, error: function(data){ ... } }); }
spring controller 中这样写:
@RequestMapping(value = "/queryInfo", method = RequestMethod.POST) @ResponseBody public List<Record> queryInfo(@RequestBody SearchCondition condition) { //...... }