springMvc 接收復雜的引數型別
Spring MVC在接收集合請求引數時,需要在Controller方法的集合引數裡前新增@RequestBody,而@RequestBody預設接收的enctype (MIME編碼)是application/json,因此傳送POST請求時需要設定請求報文頭資訊,否則Spring MVC在解析集合請求引數時不會自動的轉換成JSON資料再解析成相應的集合。以下列舉接收List<Integer>、List<User>、List<Map<String,Object>>、User[]、User(bean裡面包含List)幾種較為複雜的集合引數示例:
- 一、接收List<Integer>集合引數:
1、頁面js程式碼:
Js程式碼
var arr = [1,2,3]; $.jBox.confirm("確定要刪除資料嗎?", "warning",function () { $.ajax({ type: 'get', url: '${base.contextPath}/giving/index/del', dataType: 'json', data: {ids: arr}, success: function (result) { … }, error: function (result) { … } }) })
2、Controller方法:
Java程式碼
@Controller @RequestMapping("/wxgiving") public class WxGivingController{ @RequestMapping(value = "/index/del", method = RequestMethod.GET) @ResponseBody public ReturnMsg del (@RequestParam(value = "ids[]")List <Integer> ids){ … } }
- 接收List<User>、User[]集合引數:
1、User實體類:
Java程式碼
public class User { private int id; private String name; private String pwd; //省略getter/setter }
2、頁面js程式碼:
Js程式碼
//可以找前端拼成這種型別 var userList = new Array(); userList.push({name: "張三",pwd: "123"}); userList.push({name: "李四",pwd: "223"}); $.ajax({ type: "POST", url: "${base.contextPath}/user/index/add", data: JSON.stringify(userList),//將物件序列化成JSON字串 dataType:"json", contentType : 'application/json;charset=utf-8', //設定請求頭資訊 success: function(result){ … }, error: function(result){ … } });
3、Controller方法:
Java程式碼
@Controller @RequestMapping(value = "/user") public class UserController(){ @RequestMapping(value = "/index/add", method = RequestMethod.POST) @ResponseBody public ReturnMsg addOrEdit(@RequestBody List<User> userList) { … } }
如果想要接收User[]陣列,只需要把add的引數型別改為@RequestBody User[] userArray就行了。
- 接收List<Map<String,Object>>集合引數:
1、頁面js程式碼(不需要User物件了):
Js程式碼
-
var userList = new Array(); userList.push({name: "張三",pwd: "123"}); userList.push({name: "李四",pwd: "223"}); $.ajax({ type: "POST", url: "${base.contextPath}/user/index/add", data: JSON.stringify(userList),//將物件序列化成JSON字串 dataType:"json", contentType : 'application/json;charset=utf-8', //設定請求頭資訊 success: function(result){ … }, error: function(result){ … } });
2、Controller方法:
Java程式碼
-
@Controller @RequestMapping(value = "/user") public class UserController(){ @RequestMapping(value = "/index/add", method = RequestMethod.POST) @ResponseBody public ReturnMsg addOrEdit(@RequestBody List<Map<String,Object>> listMap) { … } }
- 接收User(bean裡面包含List)集合引數:
1、User實體類:
Java程式碼
-
public class User { private int id; private String name; private String pwd; private List<User> userList; //省略getter/setter }
2、頁面js程式碼:
Js程式碼
-
var userArray= new Array(); userArray.push({name: "張三",pwd: "123"}); userArray.push({name: "李四",pwd: "223"}); var user = {}; user.name = "王五"; user.pwd = "888"; user.userList= userArray; $.ajax({ type: "POST", url: "${base.contextPath}/user/index/add", data: JSON.stringify(user),//將物件序列化成JSON字串 dataType:"json", contentType : 'application/json;charset=utf-8', //設定請求頭資訊 success: function(result){ … }, error: function(result){ … } });
3、Controller方法:
Java程式碼
-
@Controller @RequestMapping(value = "/user") public class UserController(){ @RequestMapping(value = "/index/add", method = RequestMethod.POST) @ResponseBody public ReturnMsg addOrEdit(@RequestBody User user) { List<User> userList= user.getUserList();
相關推薦
springMvc 接收復雜的引數型別
Spring MVC在接收集合請求引數時,需要在Controller方法的集合引數裡前新增@RequestBody,而@RequestBody預設接收的enctype (MIME編碼)是application/json,因此傳送POST請求時需要設定請求報文頭資訊,否則Spr
SpringMVC接收復雜集合引數
在接收集合請求引數時,需要在Controller方法的集合引數裡前新增@RequestBody,而@RequestBody預設接收的enctype (MIME編碼)是application/json,因此傳送POST請求時需要設定請求報文頭資訊,否則Spring MV
SpringMVC接收復雜集合引數【轉載】
宣告:本部落格轉載於博主“jxd_zxf”的部落格,原文章連結:http://jxd-zxf.iteye.com/blog/2072300/Spring MVC在接收集合請求引數時,需要在Controller方法的集合引數裡前新增@RequestBody,而@RequestB
SpringMVC接收復雜對象
技術分享 解析 img att 題目 spring bsp mat 高中 SpringMVC接收復雜對象 轉載請註明地址:http://www.cnblogs.com/funnyzpc/p/7642977.html 本節內容暫放一邊,我先扯點兒,心情好了,代碼順風順水哈
Spring mvc接收復雜資料型別
**前言:**Spring MVC在接收集合請求引數時,需要在Controller方法的集合引數裡前新增@RequestBody,而@RequestBody預設接收的enctype (MIME編碼)是application/json,因此傳送POST請求時需要設定請求報文頭資訊,否則Spri
springmvc接收復雜型別的json資料需要注意的地方
1.如果接收的是普通的資料型別或者單物件 後端的方法上面什麼也不需要處理,只需要將物件放入到引數上面就可以了 2.如果接收的是複雜型別 例如物件裡面還包含物件或者是集合等,後端的方法引數上面只需要加
Springmvc controller接收請求引數型別
轉載地址:http://18810098265.iteye.com/blog/2380269 第一種情況:資料是基本型別或者String1, 直接用表單提交,引數名稱相同即可; Controller引數定義為陣列型別即可.不要定義為List<String>
SpringMVC的引數型別轉換
package com.controller; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.format.ann
java中的日期轉換、springmvc接收前臺的Date型別引數遇到的坑
關於springmvc接收前臺傳的時間型別引數 前臺jsp用的一個日期外掛,後臺獲取一直有問題。 被這個問題搞了好久,其實很簡單。記錄下來,希望可以幫到遇到同樣問題的同學。 我專案使用的ssm框架, 在做web開發的時候,頁面傳入的都是String型別,SpringMVC可以對一些基本的型別
使用springmvc從頁面中獲取資料,然後根據獲得的引數資訊進行修改,如果修改的資料中含有不是基本資料型別的引數。比如傳的引數中有Date型別的資料時,需要我們進行引數型別轉換。
1.1 需求 在商品修改頁面可以修改商品的生產日期,並且根據業務需求自定義日期格式。 1.2 需求分析 由於日期資料有很多格式,所以springmvc沒辦法把字串轉換成日期型別。所以需要自定義引數繫結。前端控制器接收到請求後,找到註解形式的處理器介面卡,對RequestMapping標記的方法進
SpringMVC之繫結引數的型別轉換(Date/Double)
一、使用註解式控制器註冊PropertyEditor(針對具體的controller類處理) 1、使用WebDataBinder進行控制器級別的註冊PropertyEditor(控制器獨享) Java程式碼 @InitBinder // 此
SpringMVC利用json接收復雜物件和陣列
1,SpringMVC接收復雜引數必須要在引數前加 @RequestBody 2,ajax設定引數 contentType: 'application/json;charset=utf-8'(設定傳送資料型別為json) data: JSON.stringify(物件
springmvc接口接收json類型參數設置
edi alt per 不能 convert ping ota pan art Springmvc需要如下配置: 1.開啟註解 <!-- 開啟註解--> <mvc:annotation-driven /> 2.加入相關bean <
springmvc接口ios網絡請求
es2017 ica pri log nbsp mvc http alt .cn springmvc: application/json;charset=utf-8的ios網絡請求: 後臺使用 @RequestBody註解參數接收: springmvc接口ios網絡
SpringMVC接收路徑/url引數
SpringMVC接收路徑/url引數 專案結構 接收路徑引數 編寫action @Controller @RequestMapping("/") public class HelloController { @RequestMappi
SpringMVC中Pojo作為引數的應用
首先在頁面新增一個表單. <form action="springmvc/testPojo" method="post"> <input type="text" name="username" /> <br>
SpringMVC入門丶請求引數繫結丶常用註解
SpringMVC入門 建立WEB工程,引入依賴 <!-- 版本鎖定 --> <properties> <spring.version>5.0.2.RELEASE</spring.version> </properties> &
springboot accep定義 (接受+返回) 的引數型別
1.返回的引數json轉xml 1.匯入包 <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-d
MyBatis呼叫帶有返回結果之output引數型別的儲存過程
儲存過程示例: ALTER PROCEDURE [WebApi_Login_SendPhoneCode ] ( @Action int, --1:查詢 2:新增一條記錄 @PhoneNo varchar(20), @Smscode varchar(10), @SendType
利用模板實現IfThenElse,選擇引數型別
在模板開發中,有時需要根據實際選擇函式返回值的型別,特別是在型別需要提升的情況下。 下面提供從兩個引數型別中選擇其中一個型別的模板類,採用了基本模板和區域性特化技術實現。其實現如下: //base template template<bool C, typename T1, typ