ADBLOCK비활성화

ADBlock이 사이트의 일부 콘텐츠를 차단하고 있습니다.

ADBlock errore

Routes in play2: How to match a part of the url

StackOverflow https://stackoverflow.com/questions/9312148

질문

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(_ != '.')
  ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow