有一些网址,例如:
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/