在@RequestParam中绑定列表

发布于 2024-10-10 00:31:17 字数 941 浏览 3 评论 0原文

我以这种方式从表单发送一些参数:

myparam[0]     : 'myValue1'
myparam[1]     : 'myValue2'
myparam[2]     : 'myValue3'
otherParam     : 'otherValue'
anotherParam   : 'anotherValue' 
...

我知道我可以通过添加参数来获取控制器方法中的所有参数,就像

public String controllerMethod(@RequestParam Map<String, String> params){
    ....
}

我想将参数 myParam[] (而不是其他参数)绑定到列表或数组(任何保持索引顺序的东西),所以我尝试使用如下语法:

public String controllerMethod(@RequestParam(value="myParam") List<String> myParams){
    ....
}

public String controllerMethod(@RequestParam(value="myParam") String[] myParams){
    ....
}

它们都没有绑定 myParams。即使当我向映射添加值时,它也无法绑定参数:

public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){
    ....
}

是否有任何语法可以将某些参数绑定到列表或数组,而不必创建一个带有列表属性的对象作为 @ModelAttribute ?

谢谢

I'm sending some parameters from a form in this way:

myparam[0]     : 'myValue1'
myparam[1]     : 'myValue2'
myparam[2]     : 'myValue3'
otherParam     : 'otherValue'
anotherParam   : 'anotherValue' 
...

I know I can get all the params in the controller method by adding a parameter like

public String controllerMethod(@RequestParam Map<String, String> params){
    ....
}

I want to bind the parameters myParam[] (not the other ones) to a list or array (anything that keeps the index order), so I've tried with a syntax like:

public String controllerMethod(@RequestParam(value="myParam") List<String> myParams){
    ....
}

and

public String controllerMethod(@RequestParam(value="myParam") String[] myParams){
    ....
}

but none of them are binding the myParams. Even when I add a value to the map it is not able to bind the params:

public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){
    ....
}

Is there any syntax to bind some params to a list or array without having to create an object as @ModelAttribute with a list attribute in it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

北渚 2024-10-17 00:31:17

或者你可以这样做:

public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
    ....
}

例如,这适用于这样的表单:

<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />

这是最简单的解决方案:)

Or you could just do it that way:

public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
    ....
}

That works for example for forms like this:

<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />

This is the simplest solution :)

傾旎 2024-10-17 00:31:17

@RequestParam 中的数组用于绑定多个同名参数:

myparam=myValue1&myparam=myValue2&myparam=myValue3

如果你需要绑定 @ModelAttribute 风格的索引参数,我猜你需要 @ModelAttribute无论如何。

Arrays in @RequestParam are used for binding several parameters of the same name:

myparam=myValue1&myparam=myValue2&myparam=myValue3

If you need to bind @ModelAttribute-style indexed parameters, I guess you need @ModelAttribute anyway.

迷乱花海 2024-10-17 00:31:17

订阅 basil 在问题本身的评论中所说的话,如果 method = RequestMethod.GET 你可以使用 @RequestParam List;组值

然后使用参数列表调用服务非常简单:

API_URL?groupVal=kkk,ccc,mmm

Subscribing what basil said in a comment to the question itself, if method = RequestMethod.GET you can use @RequestParam List<String> groupVal.

Then calling the service with the list of params is as simple as:

API_URL?groupVal=kkk,ccc,mmm
我恋#小黄人 2024-10-17 00:31:17

只是补充 Donal Fellows 所说的,您可以使用 List 和 @RequestParam

public String controllerMethod(@RequestParam(value="myParam") List<ObjectToParse> myParam){
....
}

希望它有帮助!

Just complementing what Donal Fellows said, you can use List with @RequestParam

public String controllerMethod(@RequestParam(value="myParam") List<ObjectToParse> myParam){
....
}

Hope it helps!

日裸衫吸 2024-10-17 00:31:17

实现此目的的一种方法(以一种黑客的方式)是为 List 创建一个包装类。像这样:

class ListWrapper {
     List<String> myList; 
     // getters and setters
}

那么你的控制器方法签名将如下所示:

public String controllerMethod(ListWrapper wrapper) {
    ....
}

如果您在请求中传递的集合名称与集合匹配,则无需使用 @RequestParam@ModelAttribute 注释包装类的字段名称,在我的示例中,您的请求参数应如下所示:

myList[0]     : 'myValue1'
myList[1]     : 'myValue2'
myList[2]     : 'myValue3'
otherParam    : 'otherValue'
anotherParam  : 'anotherValue'

One way you could accomplish this (in a hackish way) is to create a wrapper class for the List. Like this:

class ListWrapper {
     List<String> myList; 
     // getters and setters
}

Then your controller method signature would look like this:

public String controllerMethod(ListWrapper wrapper) {
    ....
}

No need to use the @RequestParam or @ModelAttribute annotation if the collection name you pass in the request matches the collection field name of the wrapper class, in my example your request parameters should look like this:

myList[0]     : 'myValue1'
myList[1]     : 'myValue2'
myList[2]     : 'myValue3'
otherParam    : 'otherValue'
anotherParam  : 'anotherValue'
洒一地阳光 2024-10-17 00:31:17

对我来说,虽然您可以接受集合作为请求参数,但在消费者方面您仍然必须将集合项作为逗号分隔值传递,这对我来说并不明显。

例如,如果服务器端 api 如下所示:

@PostMapping("/post-topics")
public void handleSubscriptions(@RequestParam("topics") Collection<String> topicStrings) {

    topicStrings.forEach(topic -> System.out.println(topic));
}

直接将集合作为 RequestParam 传递到 RestTemplate,如下将导致数据损坏

public void subscribeToTopics() {

    List<String> topics = Arrays.asList("first-topic", "second-topic", "third-topic");

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForEntity(
            "http://localhost:8088/post-topics?topics={topics}",
            null,
            ResponseEntity.class,
            topics);
}

相反,您可以使用

public void subscribeToTopics() {

    List<String> topicStrings = Arrays.asList("first-topic", "second-topic", "third-topic");
    String topics = String.join(",",topicStrings);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForEntity(
            "http://localhost:8088/post-topics?topics={topics}",
            null,
            ResponseEntity.class,
            topics);
}

完整的示例可以在这里找到,希望它能帮别人解决头痛:)

It wasn't obvious to me that although you can accept a Collection as a request param, but on the consumer side you still have to pass in the collection items as comma separated values.

For example if the server side api looks like this:

@PostMapping("/post-topics")
public void handleSubscriptions(@RequestParam("topics") Collection<String> topicStrings) {

    topicStrings.forEach(topic -> System.out.println(topic));
}

Directly passing in a collection to the RestTemplate as a RequestParam like below will result in data corruption

public void subscribeToTopics() {

    List<String> topics = Arrays.asList("first-topic", "second-topic", "third-topic");

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForEntity(
            "http://localhost:8088/post-topics?topics={topics}",
            null,
            ResponseEntity.class,
            topics);
}

Instead you can use

public void subscribeToTopics() {

    List<String> topicStrings = Arrays.asList("first-topic", "second-topic", "third-topic");
    String topics = String.join(",",topicStrings);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForEntity(
            "http://localhost:8088/post-topics?topics={topics}",
            null,
            ResponseEntity.class,
            topics);
}

The complete example can be found here, hope it saves someone the headache :)

江湖正好 2024-10-17 00:31:17

使用复选框切换更改隐藏字段值,如下所示...

HTML:

<input type='hidden' value='Unchecked' id="deleteAll" name='anyName'>
<input type="checkbox"  onclick="toggle(this)"/> Delete All

脚本:

function toggle(obj) {`var $input = $(obj);
    if ($input.prop('checked')) {

    $('#deleteAll').attr( 'value','Checked');

    } else {

    $('#deleteAll').attr( 'value','Unchecked');

    }

}

Change hidden field value with checkbox toggle like below...

HTML:

<input type='hidden' value='Unchecked' id="deleteAll" name='anyName'>
<input type="checkbox"  onclick="toggle(this)"/> Delete All

Script:

function toggle(obj) {`var $input = $(obj);
    if ($input.prop('checked')) {

    $('#deleteAll').attr( 'value','Checked');

    } else {

    $('#deleteAll').attr( 'value','Unchecked');

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文