springmvc參數(shù)為對象,數(shù)組的操作
更新時間:2021年08月20日 08:48:32 作者:shuair
這篇文章主要介紹了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)文章
Springboot集成百度地圖實現(xiàn)定位打卡的示例代碼
本文主要介紹了Springboot集成百度地圖實現(xiàn)定位打卡的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2024-02-02

