Routes in play2: How to match a part of the url
-
26-10-2019 - |
문제
There are some urls like:
http://localhost:9000/images/111111.jpg
http://localhost:9000/images/222222.png
http://localhost:9000/images/333333.gif
They will be mapped to a method:
def showImage(id: String) = Action {
val image = Image.findById(id).get
Ok.sendFile(new File(image.path)
}
Note that the id
is the only part of filename showed in url: 111111
, 222222
, 333333
So I write a mapping in routes:
GET /images/$id<\w+>.* controllers.Images.showImage(id)
In the part $id<\w+>.*
, id
is matching the id, and .*
match the suffix which will be ignored.
But the syntax is incorrect, the error message is:
Identifier expected
How to fix it?
해결책
It’s not currently possible to do that with Play 2. As a workaround you can process your argument in the controller action:
GET /images/:id controllers.Images.showImage(id)
def showImage(idWithExt: String) = Action { val id = idWithExt.takeWhile(_ != '.') ... }
제휴하지 않습니다 StackOverflow