java 以html形式传递字符串数组并提交给Java Spring Controller?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38261488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing array of String in html form and submit to Java Spring Controller?
提问by Sumit Badaya
I'm having a hard time figuring out how to pass an array of values to a java spring controller method in RequestParam.
我很难弄清楚如何将一组值传递给 RequestParam 中的 java spring 控制器方法。
My html form is below:
我的 html 表单如下:
<form method="post">
<input type="text" name="numbers[]">
<input type="submit">
</form>
and my spring controller is as follows:
我的弹簧控制器如下:
@RequestMapping(value="/send", method = RequestMethod.POST)
public void sendMessage(String to[]) {
for(String number: to) {
System.out.println(number);
}
}
However when I run this, it shows an error:
但是,当我运行它时,它显示一个错误:
... is not applicable for the arguments
...不适用于参数
采纳答案by Adrien Brunelat
The problem is that your input is merely a String field, so Spring converts it as a String, not as an array of String.
问题是您的输入只是一个字符串字段,因此 Spring 将其转换为字符串,而不是字符串数组。
A solution is to have several inputs in your form with the same name. Spring automatically creates the array and passes it to the controller as such.
一个解决方案是在您的表单中有多个具有相同名称的输入。Spring 自动创建数组并将其传递给控制器。
<form method="post">
<input type="text" name="number">
<input type="text" name="number">
<input type="text" name="number">
<input type="submit">
</form>
The corresponding method in the controller would be:
控制器中的相应方法是:
public void foo(@RequestParam("number[]") List<String> to) {
for(String number : to) {
System.out.println(number);
}
}
回答by Antoniossss
If anyone is still struggling with that, this is how it should be done:
如果有人仍在为此苦苦挣扎,那么应该这样做:
Form inputs:
表单输入:
<input name="myParam" value="1"/>
<input name="myParam" value="4"/>
<input name="myParam" value="19"/>
Controller method:
控制器方法:
@RequestMapping
public String deletePlaces(@RequestParam("myParam") Long[] myParams) {
//myParams will have 3 elements with values 1,4 and 19
}
This works the same for String[]
Integer[]
Long[]
and probably more. POST
,GET
,DELETE
will work the same way.
这适用于String[]
Integer[]
Long[]
甚至更多。POST
, GET
,DELETE
将以相同的方式工作。
Parameter name must match name
tag from form input. No extra []
needed etc. In fact, param name can be ommited, if method argument name is the same as input name so we can end up with method signature like this:
参数名称必须与name
表单输入中的标签匹配。不需要额外的[]
等等。事实上,param name 可以省略,如果方法参数名称与输入名称相同,那么我们最终可以得到这样的方法签名:
@RequestMapping
public String deletePlaces(@RequestParam Long[] myParams)
and it will still work
它仍然有效
Something extra:Now if you have domain model lets say Place
and you have PlaceRepository
, by providing Place#id
as value of your inputs, Spring can lookup related objects for us. So if we assume that form inputs above contais User ids as values, then we can simply write this into controller:
额外的东西:现在,如果你有域模型Place
并且你有PlaceRepository
,通过提供Place#id
你的输入值,Spring 可以为我们查找相关的对象。因此,如果我们假设上面的表单输入包含用户 ID 作为值,那么我们可以简单地将其写入控制器:
public String deletePlaces(@RequestParam Place[] places) {
//places will be populated with related entries from database!
}
Sweet isn't it?
甜蜜是不是?