gpt4 book ai didi

play2 中的路线 : How to match a part of the url

转载 作者:行者123 更新时间:2023-12-04 02:19:30 26 4
gpt4 key购买 nike

有一些网址,例如:

http://localhost:9000/images/111111.jpg
http://localhost:9000/images/222222.png
http://localhost:9000/images/333333.gif

它们将被映射到一个方法:

def showImage(id: String) = Action {
val image = Image.findById(id).get
Ok.sendFile(new File(image.path)
}

请注意 id是 url 中显示的文件名的唯一部分:111111 , 222222 , 333333

所以我在路由中写了一个映射:

GET  /images/$id<\w+>.*          controllers.Images.showImage(id)

$id<\w+>.*部分, id与 ID 匹配,.*匹配将被忽略的后缀。

但是语法不对,错误信息是:

Identifier expected

如何解决?

最佳答案

Play 2 目前无法做到这一点。作为解决方法,您可以在 Controller 操作中处理您的参数:

GET    /images/:id       controllers.Images.showImage(id)
def showImage(idWithExt: String) = Action {  val id = idWithExt.takeWhile(_ != '.')  ...}

关于play2 中的路线 : How to match a part of the url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9312148/

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