springmvc參數(shù)為對象,數(shù)組的操作
參數(shù)為對象
1、提交表單
2、表單序列化,使用ajax提交
var data = $("#addForm").serialize();
$.ajax({
url : "addReportDo", //請求url
type : "POST", //請求類型 post|get
data : data,
dataType : "text", //返回數(shù)據(jù)的 類型 text|json|html--
success : function(result){ //回調(diào)函數(shù) 和 后臺返回的 數(shù)據(jù)
alert(result);
}
});
3、也可以這樣寫
var data = {
title: $("#title").val(),
note: $("#note").val()
};
4、如果結(jié)構(gòu)復(fù)雜,使用@RequestBody
需要引用jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
springmvc.xml配置
<!--Spring3.1開始的注解 HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!-- json轉(zhuǎn)換器 -->
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
js寫法
var goods1 = {
goodsNumber: "001",
goodsName: "商品A"
}
var goods2 = {
goodsNumber: "002",
goodsName: "商品B"
}
var goodsList = [goods1,goods2];
var data = {
title: $("#title").val(),
note: $("#note").val(),
goodsList: goodsList
};
console.log(data);
$.ajax({
url : "addReportDo", //請求url
type : "POST", //請求類型 post|get
data : JSON.stringify(data),
contentType : "application/json",
dataType : "text", //返回數(shù)據(jù)的 類型 text|json|html--
success : function(result){ //回調(diào)函數(shù) 和 后臺返回的 數(shù)據(jù)
alert(result);
}
});
注意ajax的兩個屬性,data屬性變?yōu)镴SON.stringify(data),增加contentType屬性。
controller代碼寫法
@ResponseBody
@RequestMapping("addReportDo")
public String addReportDo(@RequestBody Report report){
System.out.println(report);
return "ok";
}
在參數(shù)前面加上@RequestBody即可。
5、傳遞數(shù)組
js寫法
var array = ["a","b","c"];
var data = {
array : array
};
console.log(data);
$.ajax({
url : "addReportDo", //請求url
type : "POST", //請求類型 post|get
data : data,
dataType : "text", //返回數(shù)據(jù)的 類型 text|json|html--
success : function(result){ //回調(diào)函數(shù) 和 后臺返回的 數(shù)據(jù)
alert(result);
}
});
controller寫法
@ResponseBody
@RequestMapping("addReportDo")
public String addReportDo(@RequestParam("array[]") String[] array){
System.out.println(Arrays.toString(array));
return "ok";
}
也可以用List接收
@ResponseBody
@RequestMapping("addReportDo")
public String addReportDo(@RequestParam("array[]") List<String> list){
System.out.println(list);
return "ok";
}
springmvc接受復(fù)雜對象(對象數(shù)組)
前端:
將請求頭改為
contentType:"application/json;charset=UTF-8"
后端:
自定義一個對象,將參數(shù)封裝進該對象中
@Data
public class CaseBodyEntity {
String token;
CaseBasicModel caseBasic;
String[] keywords;
CaseInsurantAndProductModel[] caseInsurantAndProductModels;
CaseExperienceModel[] caseExperiences;
CaseAssessModel[] caseAssesses;
}
使用使用POST方式接受請求,@RequestBody接受請求參數(shù),對象為自定義的接受對象
@ApiOperation("添加或更新案例,后臺")
@PostMapping("/addOrUpdateCase")
public JSONObject addOrUpdateCase(
@RequestBody CaseBodyEntity caseBodyEntity
) {
...
}
@RequestBody和@RequestParam的區(qū)別
- @RequestParam,主要處理contentType為application/x-www-form-urlencoded的數(shù)據(jù)(默認);@ResponseBody:主要處理contentType不為application/x-www-form-urlencoded的數(shù)據(jù),例如:application/json;charset=UTF-8
- @RequestParam:要指明前端傳過來的參數(shù)名并與其對應(yīng);@RequestBody:直接對象接收,屬性名要與前端傳過來的數(shù)據(jù)的key值對應(yīng)
- 使用@RequestParam:Content-Type為application/x-www-form-urlencoded,參數(shù)在FormData中;使用@RequestBody:Content-Type為application/json,參數(shù)在Request PayLoad中
- 可以使用多個@RequestParam獲取數(shù)據(jù);@RequestBody不能在同一個方法中出現(xiàn)多次
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于maven install 沒反應(yīng)的解決方法
下面小編就為大家?guī)硪黄趍aven install 沒反應(yīng)的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Java thrift服務(wù)器和客戶端創(chuàng)建實例代碼
Thrift是一個軟件框架,用來進行可擴展且跨語言的服務(wù)的開發(fā)。接下來通過本文給大家介紹Java thrift服務(wù)器和客戶端創(chuàng)建實例代碼,需要的朋友參考下吧2017-04-04
Java如何獲取一個隨機數(shù) Java猜數(shù)字小游戲
這篇文章主要為大家詳細介紹了Java如何獲取一個隨機數(shù),類似猜數(shù)字小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
springboot數(shù)據(jù)庫密碼加密的配置方法
這篇文章主要給大家介紹了關(guān)于springboot數(shù)據(jù)庫密碼加密的配置方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringBoot如何使用Scala進行開發(fā)的實現(xiàn)
這篇文章主要介紹了SpringBoot如何使用Scala進行開發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

