3

有一些网址,例如:

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)

在part$id<\w+>.*中,id匹配id,.*匹配后缀会被忽略。

但是语法不正确,错误信息是:

Identifier expected

如何解决?

4

1 回答 1

6

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

GET /images/:id controllers.Images.showImage(id)
def showImage(idWithExt: String) = Action {
  val id = idWithExt.takeWhile(_ != '.')
  ...
}
于 2012-02-17T08:48:26.877 回答