1. 程式人生 > >Springmvc controller接收請求引數型別

Springmvc controller接收請求引數型別

轉載地址:http://18810098265.iteye.com/blog/2380269

第一種情況:資料是基本型別或者String
1, 直接用表單提交,引數名稱相同即可; 
Controller引數定義為陣列型別即可.不要定義為List<String> 

Html程式碼  收藏程式碼
  1. <form action="${pageContext.request.contextPath}/dashboard/xxx" method="post">  
  2.     <input type="text" name="xxx"/><br>  
  3.     <input type
    ="text" name="xxx"/><br>  
  4.     <input type="text" name="xxx"/><br>  
  5.     <input type="text" name="xxx"/><br>  
  6.     <input type="submit" value="tijiao"/>  
  7. </form>  


Java程式碼  收藏程式碼
  1. @RequestMapping(value = "/xxx")  
  2.    public void xxx(String[] xxx) {  
  3.        System.out.println(xxx);  
  4.    }  

Controller中不要用List<String>接收,不然會報下面這個錯org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface 

2, ajax請求: 
Js程式碼  收藏程式碼
  1. $.ajax({  
  2.         url: "${pageContext.request.contextPath}/dashboard/xxx",  
  3.         type: "POST",  
  4.         contentType : 'application/json;charset=utf-8'//請求頭一定要有  
  5.         dataType:"json",  
  6.         data: JSON.stringify(["aaa","bbb"]), //將Json物件轉成Json字串  
  7.         success: function(data){  
  8.             alert(data);  
  9.         },  
  10.         error: function(res){  
  11.             alert(res.responseText);  
  12.         }  
  13.     });  


Java程式碼  收藏程式碼
  1. @RequestMapping(value = "/xxx")  
  2. public void xxx(@RequestBody String[] xxx) {  
  3.     System.out.println(xxx);  
  4. }  

ajax請求時請求頭contentType 申明為application/json;charset=utf-8,且type 為post, 這樣請求體在body中且請求體為一個json字串, controller中獲取引數一定要加@RequestBody,表示取請求體中的所有的內容,然後轉為String[], 至於形參名xxx可以任意命名,因為application/json;charset=utf-8的請求體中無引數名稱 


第二種情況:資料是自定義物件
Java程式碼  收藏程式碼
  1. public class User {  
  2.     private Integer id;  
  3.     private String name;  
  4.     private String pwd;  
  5.     //getter、setter省略  
  6.  }  


1, 表單提交 
Html程式碼  收藏程式碼
  1. <form action="/user/submitUserList_2" method="post">  
  2.         ID:<input type="text" name="users[0].id"><br/>  
  3.         Username:<input type="text" name="users[0].name"><br/>  
  4.         Password:<input type="text" name="users[0].pwd"><br/><br/>  
  5.         ID:<input type="text" name="users[2].id"><br/>  
  6.         Username:<input type="text" name="users[2].name"><br/>  
  7.         Password:<input type="text" name="users[2].pwd"><br/><br/>  
  8.         <input type="submit" value="Submit">  
  9.     </form>  


除了剛才公用的User類,還要封裝一個User的容器類UserModel: 
Java程式碼  收藏程式碼
  1. public class UserModel {  
  2.     private List<User> users;  
  3.     public List<User> getUsers() {  
  4.         return users;  
  5.     }  
  6.     public void setUsers(List<User> users) {  
  7.         this.users = users;  
  8.     }  
  9.     public UserModel(List<User> users) {  
  10.         super();  
  11.         this.users = users;  
  12.     }  
  13.     public UserModel() {  
  14.         super();  
  15.     }  
  16. }  
Java程式碼  收藏程式碼
  1. @RequestMapping(value = "/xxx")  
  2. public void xxx(UserModel users) {  
  3.     List<User> userList = users.getUsers();  
  4. }  


2, ajax 請求 
Js程式碼  收藏程式碼
  1. var arr = new Array();  
  2. arr.push({id: "1", name: "李四", pwd: "123"});  
  3. arr.push({id: "2", name: "張三", pwd: "332"});  
  4. $.ajax({  
  5.         url: "${pageContext.request.contextPath}/dashboard/xxx",  
  6.         type: "POST",  
  7.         contentType : 'application/json;charset=utf-8'//請求頭一定要加  
  8.         dataType:"json",  
  9.         data: JSON.stringify(arr),    //將Json物件序列化成Json字串  
  10.         success: function(data){  
  11.             alert(data);  
  12.         },  
  13.         error: function(res){  
  14.             alert(res.responseText);  
  15.         }  
  16.     });  

Java程式碼  收藏程式碼
  1. @RequestMapping(value = "/xxx")  
  2. public void xxx(@RequestBody List<User> xxx) {  
  3.     System.out.println(xxx);  
  4. }  

這裡可以定義成List<User>, 不會報異常.ajax中的contentType 和controller中的@RequestBody一定要加 


第三種,適宜任何型別資料
用js將請求引數轉為json字串, 然後contentType 設定為application/x-www-form-urlencoded, 這種請求的請求體格式是name1=value1&name2=value2&... 

Js程式碼  收藏程式碼
  1. var arr = new Array();  
  2. arr.push({id: "1", name: "李四", pwd: "123"});  
  3. arr.push({id: "2", name: "張三", pwd: "332"});  
  4. $.ajax({  
  5.         url: "${pageContext.request.contextPath}/dashboard/xxx",  
  6.         type: "POST",  
  7.         contentType : 'application/x-www-form-urlencoded'//設定請求頭資訊  
  8.         dataType:"json",  
  9.         data: "xxx="+JSON.stringify(customerArray), //將Json物件序列化成Json字串  
  10.         success: function(data){  
  11.             alert(data);  
  12.         },  
  13.         error: function(res){  
  14.             alert(res.responseText);  
  15.         }  
  16.     });  

Java程式碼  收藏程式碼
  1. @RequestMapping(value = "/xxx")  
  2.     public void xxx(String xxx) {  
  3.         System.out.println(xxx);  
  4.        //用Gson或其他json包轉成物件或陣列  
  5.     }  
注意:如果 contentType 設定為 application/json;charset=utf-8, controller引數要加@RequestBody,否則取不到值

相關推薦

Springmvc controller接收請求引數型別

轉載地址:http://18810098265.iteye.com/blog/2380269 第一種情況:資料是基本型別或者String1, 直接用表單提交,引數名稱相同即可; Controller引數定義為陣列型別即可.不要定義為List<String> 

SpringMVC接收請求引數和頁面傳參

1.Spring接收請求引數 1>.使用HttpServletRequest獲取 @RequestMapping("/login.do") public String login(HttpServletRequest request){ String name = request.g

springmvc Controller接收前端引數的幾種方式總結

  (1) 普通方式-請求引數名和Controller方法的引數一致   1 @Controller 2 @RequestMapping("/param") 3 public class TestParamController { 4 private static fin

springmvc中@requestbody註解接收請求引數

一、POST請求的四種常用方式   1、application/x-www-form-urlencoded     瀏覽器原生的表單,值為urlencoded之後的  key1=value1&key2=value2......   2、multipart/form-data     

springmvc controller接收jsp頁面傳過來的引數和傳值到jsp頁面

接收值--四種方法: 第一種:引數直接寫在controller引數列表中 @RequestMapping("/test1.action")     public ModelAndView test1(String name){         System.out.prin

SpringMVC——接收請求引數和頁面傳參

原文地址:http://blog.csdn.net/z69183787/article/details/41653875點選開啟連結 Spring接收請求引數: 1,使用HttpServletRequest獲取 Java程式碼   @RequestMapp

當配置了filter後SpringMVC Controller 接收頁面傳遞的中文引數還出現亂碼時,要修改tomcat的server.xml配置檔案

新配置一個spring的MVC專案,發現對Get請求的中文引數出現了亂碼:查看了SpingMVC中關於編碼的配置(在web.xml中),如下:<filter>        <filter-name>encodingFilter</filter-

spring-05-Controller如何接收請求引數

1.利用HttpServletRequest 2.利用業務方法引數 –引數名與請求引數key保持一致 –利用@RequestParam(“key”) login.do?username=xxx public String f1(@Req

SpringMVC入門丶請求引數繫結丶常用註解

SpringMVC入門 建立WEB工程,引入依賴 <!-- 版本鎖定 --> <properties> <spring.version>5.0.2.RELEASE</spring.version> </properties> &

egg接收請求引數

1、get請求 let query = this.ctx.query; let name = query.name; let id = query.id; 2、post請求 let query = this.ctx.request.body; let name = query.nam

學習SpringMVC——如何獲取請求引數

@RequestParam @PathVariable @QueryParam @CookieValue @ModelAndView @ModelAttribute   一、spring mvc如何匹配請求路徑——“請求路徑哪家強,RequestMapping名遠揚” @R

SpringMVC中post請求引數註解@requestBody使用問題

  一、httpClient傳送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html 1 public static String httpPostWithJSON(String url) throws Exception { 2

controller接收列表引數,List

$.ajax請求加請求頭contentType: 'application/json;charset=utf-8' var data =[]; data.push({username: "yss"}); $.ajax({ type: "post",

轉:axis客戶端接收不同引數型別

axis只支援簡單型別的返回值。在這裡逐一介紹axis的各種返回值接受。 1:axis接受基本型別,如int ,string等 引入的系統檔案: import javax.xml.namespace.QName; import javax.xml.rpc.Paramet

SpringMVC-3 對映請求引數請求

  Spring MVC通過分析控制器處理方法的簽名,將 HTTP請求資訊繫結到處理方法的相應人蔘中。除@PathVariable註解外,SpringMVC還可使用@RequestParam、@Requ

Struts2之action接收請求引數

1. 採用基本型別接受請求引數(get/post) action: public class GetparamAction extends ActionSupport {private int age;private String name;public String ge

SpringMVC之GET請求引數中文亂碼

只怪自己專案做太少,遇到這些問題糾結太久,浪費時間太多. 在此記錄, WEB.XML檔案中的編碼過濾器設定是針對POST請求的,tomacat對GET和POST請求處理方式是不同的,要處理針對GET請求的編碼問題,則需要改tomcat,conf目錄下的server.x

Java Spring Controller 獲取請求引數的幾種方法詳解

1、直接把表單的引數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若”Content-Type”=”application/x-www-form-urlencoded”,可用post提交 /** * 1.

axis客戶端接收不同引數型別

axis只支援簡單型別的返回值。在這裡逐一介紹axis的各種返回值接受。 1:axis接受基本型別,如int ,string等 引入的系統檔案: import javax.xml.namespace.QName; import javax.xml.rpc.Parameter

Retrofit 請求引數型別

Retrofit 請求網路時,使用的okhttp框架,所以除了Retrofit api 通過註解的方式新增引數型別外,最後都是進入到 了okhttp來處理。 四大型別:紅色部分為新增部分 http: