Spring MVC @RequestParam一个对象列表 [英] Spring MVC @RequestParam a list of objects
问题描述
我想创建一个页面,其中一个人看到一个用户列表,并且每个人旁边都有一个复选框,该人可以点击该复选框将其删除。
<在我使用REST API的MVC中,我想向REST API发送一个
用户
对象列表。 @RequestParam
注释可以支持吗?
例如:
@RequestMapping(method = RequestMethod.DELETE,value =/ delete)
public @ResponseBody整数删除(
@RequestParam(用户)列表<用户>列表){
整数deleteCount = 0;
for(User u:list){
if(u!= null){
repo.delete(u);
++ deleteCount;
}
}
return deleteCount;
}
在MVC客户端中,网址为:
列表list = new ArrayList< User>();
....
String url =http:// restapi / delete?users =+ list;
请求参数是字符串到字符串的多重映射。您不能将复杂对象作为请求参数传递。
但如果您只是传递应该有效的用户名 - 请参阅如何使用spring mvc使用@RequestParam捕获多个参数?
@RequestParam(users)List< String> list
但我认为最好只使用请求体传递信息。
I want to create a page where a person sees a list of users and there are check boxes next to each of them that the person can click to have them deleted.
In my MVC that consumes a REST API, I want to send a List of User
objects to the REST API.
Can the @RequestParam
annotation support that?
For example:
@RequestMapping(method = RequestMethod.DELETE, value = "/delete")
public @ResponseBody Integer delete(
@RequestParam("users") List<Users> list) {
Integer deleteCount = 0;
for (User u : list) {
if (u != null) {
repo.delete(u);
++deleteCount;
}
}
return deleteCount;
}
In the MVC client, the url would be:
List list = new ArrayList<User>();
....
String url = "http://restapi/delete?users=" + list;
Request parameters are a Multimap of String to String. You cannot pass a complex object as request param.
But if you just pass the username that should work - see how to capture multiple parameters using @RequestParam using spring mvc?
@RequestParam("users") List<String> list
But I think it would be better to just use the request body to pass information.
这篇关于Spring MVC @RequestParam一个对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!