springmvc接收数组类型参数

  • Post author:
  • Post category:其他


第一种方式:


<


script


type


=



“text/javascript”



>





function



get(){





var



privailegeIds = [


“1-1”


,


“1-3”


,


“1-4”


];


$.ajax({


url:


” ”


,


type:


“POST”


,


data:{



“peopleId”


:2,


“privailegeIds”


:privailegeIds},


success:



function



(result){


alert(JSON.stringify(result));


}});


}


</


script


>



@RequestMapping


(value = {


“”


})




@ResponseBody





public



DtoResult<Integer> updatePeoplePrivilege(Integer


peopleId


,


@RequestParam


(value =


“privailegeIds[]”


) String[]


privailegeIds


) {


DtoResult<Integer>


result


=



null



;






return



result


;


}



利用

@RequestParam


(value =


“privailegeIds[]”


)


来接收

第二种方式:


<


script


type


=



“text/javascript”



>





function



get(){





var



privailegeIds = [


“1-1”


,


“1-3”


,


“1-4”


];


$.ajax({


url:


” ”


,


type:


“POST”


,


traditional :



true



,


data:{



“peopleId”


:2,


“privailegeIds”


:privailegeIds},


success:



function



(result){


alert(JSON.stringify(result));


}});


}


</


script


>



traditional默认值是false,此处设置 traditional为true,发送数组。


@RequestMapping


(value = {


“”


})




@ResponseBody





public



DtoResult<Integer> updatePeoplePrivilege(Integer


peopleId


, String[]


privailegeIds


) {


DtoResult<Integer>


result


=



null



;






return



result


;


}

接收对象数组

参考文档:https://blog.csdn.net/alan_alei/article/details/73361104

只接收复杂的对象数组:

这里用到解析json的形式接收对象数组,为什么不直接像上面一样直接接收对象数组,因为spring接收的数组好像只能是基本类型,像这种对象类型的接受不成功,报以下错误:


Failed to convert value of type [java.lang.String[]] to required type [com.nv.common.model.AnswerInfo[]];


前端代码:


<


script


type


=



“text/javascript”



>



function



go() {



var



answerInfos = [ {


“answerContent”


:





是正确的





,


“answerTrue”


:


“Y”


,


“answerComment”


:





测试啊




},


{


“answerContent”


:





是错误的





,


“answerTrue”


:


“N”


,


“answerComment”


:





测试啊




} ];


$.ajax({


url :


“/droneSchool/exercises/insertExercisesInfo”


,


type :


“POST”


,


contentType:


‘application/json’


,


data:JSON.stringify(answerInfos),


success :



function



(result) {


}


});


}


</


script


>

这里注意:data后面传递的数据是:data:JSON.stringify(answerInfos),不能是:data:{“answerInfos”:JSON.stringify(answerInfos)},


Controller


代码:


@Controller


@RequestMapping


(value = {


“/exercises”


})



public




class



ExercisesInfoController {


@RequestMapping


(value={



“/insertExercisesInfo”


})


@ResponseBody



public



DtoResult<Integer> insertExercisesInfo(


@RequestBody


AnswerInfo[]


answerInfos


) {



return




null



;


}


}

接收对象数组和其他参数



第一种方式


:将多个参数封装到一个对象里进行接收:


前端代码:


<


script


type


=



“text/javascript”



>



function



go() {



var



exercisesInfo = {


sectionId :


“1”


,


exerciseContent :





是否正确?”


,


exerciseComment :





测试”


};



var



answerInfos = [ {


“answerContent”


:





是正确的”


,


“answerTrue”


:


“Y”


,


“answerComment”


:





测试啊”


},


{


“answerContent”


:





是错误的”


,


“answerTrue”


:


“N”


,


“answerComment”


:





测试啊”


} ];



var



exercisesAnswerDto={


“answerInfos”


:answerInfos,


“exercisesInfo”


:exercisesInfo


};


console.log(JSON.stringify(answerInfos));


$.ajax({


url :


“/droneSchool/exercises/insertExercisesInfo”


,


type :


“POST”


,


traditional :



true



,


contentType:


‘application/json’


,


data:JSON.stringify(exercisesAnswerDto),


success :



function



(result) {


}


});


}


</


script


>


封装类:



public




class



ExercisesAnswerDto {


/**


习题**/



private



ExercisesInfo


exercisesInfo


;


/**


答案列表**/



private



List<AnswerInfo>


answerInfos


;


Controller


代码:


@Controller


@RequestMapping


(value = {


“/exercises”


})



public




class



ExercisesInfoController {


@RequestMapping


(value={



“/insertExercisesInfo”


})


@ResponseBody



public



DtoResult<Integer> insertExercisesInfo(


@RequestBody


ExercisesAnswerDto exercisesAnswerDto) {



return




null



;


}


}



第二种方式


:将多个参数以字符串形式接收:


前端代码:


<


script


type


=



“text/javascript”



>



function



go() {



var



exercisesInfo = {


sectionId :


“1”


,


exerciseContent :





是否正确?”


,


exerciseComment :





测试”


};



var



answerInfos = [ {


“answerContent”


:





是正确的”


,


“answerTrue”


:


“Y”


,


“answerComment”


:





测试啊”


},


{


“answerContent”


:





是错误的”


,


“answerTrue”


:


“N”


,


“answerComment”


:





测试啊”


} ];


$.ajax({


url :


“/droneSchool/exercises/insertExercisesInfo”


,


type :


“POST”


,


data : {


answerInfos : JSON.stringify(answerInfos),


exercisesInfo : JSON.stringify(exercisesInfo)


},


success :



function



(result) {


}


});


}


</


script


>


Controller




@Controller


@RequestMapping


(value = {


“/exercises”


})



public




class



ExercisesInfoController {


@RequestMapping


(value={



“/insertExercisesInfo”


})


@ResponseBody



public



DtoResult<Integer> insertExercisesInfo(String


answerInfos


,String


exercisesInfo


) {



return




null



;


}


}



版权声明:本文为m0_37240925原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。