gpt4 book ai didi

scala - 如何在 scalatra 中传递多参数

转载 作者:行者123 更新时间:2023-12-04 16:07:10 26 4
gpt4 key购买 nike

如果我想在 scalatra 中读取 get 请求中的单个参数,我可以按如下方式进行:

get("mypath/:id") {
val id = params("id")
...
}

根据 scalatra 文档,我还可以使用 multiParams 获取一系列参数:

val ids = multiParams("ids")

但它没有说明如果我希望传递多个参数,URL 应该如何构成。那么,如果我想传递多个 ID,URL 的格式是什么?

我试过使用 & 符号、逗号和分号,但无济于事:例如

../mypath/id1&id2

最佳答案

查看文档:http://scalatra.org/guides/2.4/http/routes.html

As an example, let’s hit a URL with a GET like this:

/articles/52?foo=uno&bar=dos&baz=three&foo=anotherfoo

Look closely: there are two “foo” keys in there.

Assuming there’s a matching route at /articles/:id, we get the following results inside the action:

get("/articles/:id") {
params("id") // => "52"
params("foo") // => "uno" (discarding the second "foo" parameter value)
params("unknown") // => generates a NoSuchElementException
params.get("unknown") // => None - this is what Scala does with unknown keys in a Map

multiParams("id") // => Seq("52")
multiParams("foo") // => Seq("uno", "anotherfoo")
multiParams("unknown") // => an empty Seq
}

因此您需要为每个参数命名。例如/mypath/?ids=id1&ids=id2&ids=id3

关于scala - 如何在 scalatra 中传递多参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48264204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com