Play2中的路线:如何匹配URL的一部分
-
26-10-2019 - |
题
有一些URL,例如:
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进行操作。作为解决方法,您可以在控制器操作中处理您的论点:
GET /images/:id controllers.Images.showImage(id)
def showImage(idWithExt: String) = Action { val id = idWithExt.takeWhile(_ != '.') ... }
不隶属于 StackOverflow