好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springmvc参数为对象,数组的操作

参数为对象

1、提交表单

2、表单序列化,使用ajax提交

?

1

2

3

4

5

6

7

8

9

10

var data = $( "#addForm" ).serialize();

$.ajax({

          url : "addReportDo" ,   //请求url

          type : "POST" ,  //请求类型  post|get

          data : data,

          dataType : "text" ,  //返回数据的 类型 text|json|html--

          success : function (result){  //回调函数 和 后台返回的 数据

              alert(result);

          }

      });

3、也可以这样写

?

1

2

3

4

var data = {

     title: $( "#title" ).val(),

     note: $( "#note" ).val()

};

4、如果结构复杂,使用@RequestBody

需要引用jackson

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

< 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配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!--Spring3.1开始的注解 HandlerAdapter -->

< bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >

     <!-- json转换器 -->

     < 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写法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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" ,  //返回数据的 类型 text|json|html--

     success : function (result){  //回调函数 和 后台返回的 数据

         alert(result);

     }

});

注意ajax的两个属性,data属性变为JSON.stringify(data),增加contentType属性。

controller代码写法

?

1

2

3

4

5

6

@ResponseBody

@RequestMapping ( "addReportDo" )

public String addReportDo( @RequestBody Report report){

     System.out.println(report);

     return "ok" ;

}

在参数前面加上@RequestBody即可。

5、传递数组

js写法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

var array = [ "a" , "b" , "c" ];

var data = {

     array : array

};

console.log(data);

$.ajax({

     url : "addReportDo" ,   //请求url

     type : "POST" ,  //请求类型  post|get

     data : data,

     dataType : "text" ,  //返回数据的 类型 text|json|html--

     success : function (result){  //回调函数 和 后台返回的 数据

         alert(result);

     }

});

controller写法

?

1

2

3

4

5

6

@ResponseBody

@RequestMapping ( "addReportDo" )

public String addReportDo( @RequestParam ( "array[]" )  String[] array){

     System.out.println(Arrays.toString(array));

     return "ok" ;

}

也可以用List接收

?

1

2

3

4

5

6

@ResponseBody

@RequestMapping ( "addReportDo" )

public String addReportDo( @RequestParam ( "array[]" ) List<String> list){

    System.out.println(list);

    return "ok" ;

}

springmvc接受复杂对象(对象数组)

前端:

将请求头改为

?

1

contentType: "application/json;charset=UTF-8"

后端:

自定义一个对象,将参数封装进该对象中

?

1

2

3

4

5

6

7

8

9

@Data

public class CaseBodyEntity {

     String token;

     CaseBasicModel caseBasic;

     String[] keywords;

     CaseInsurantAndProductModel[] caseInsurantAndProductModels;

     CaseExperienceModel[] caseExperiences;

     CaseAssessModel[] caseAssesses;

}

使用使用POST方式接受请求,@RequestBody接受请求参数,对象为自定义的接受对象

?

1

2

3

4

5

6

7

@ApiOperation ( "添加或更新案例,后台" )

@PostMapping ( "/addOrUpdateCase" )

public JSONObject addOrUpdateCase(

         @RequestBody CaseBodyEntity caseBodyEntity

) {

     ...

}

@RequestBody和@RequestParam的区别

@RequestParam,主要处理contentType为application/x-www-form-urlencoded的数据(默认);@ResponseBody:主要处理contentType不为application/x-www-form-urlencoded的数据,例如:application/json;charset=UTF-8 @RequestParam:要指明前端传过来的参数名并与其对应;@RequestBody:直接对象接收,属性名要与前端传过来的数据的key值对应 使用@RequestParam:Content-Type为application/x-www-form-urlencoded,参数在FormData中;使用@RequestBody:Content-Type为application/json,参数在Request PayLoad中 可以使用多个@RequestParam获取数据;@RequestBody不能在同一个方法中出现多次

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/shuair/article/details/81212603

查看更多关于springmvc参数为对象,数组的操作的详细内容...

  阅读:19次