Question

Il y a quelques urls comme:

http://localhost:9000/images/111111.jpg
http://localhost:9000/images/222222.png
http://localhost:9000/images/333333.gif

Ils seront mis en correspondance avec une méthode:

def showImage(id: String) = Action {
    val image = Image.findById(id).get
    Ok.sendFile(new File(image.path)
}

Notez que la id est la seule partie du nom de fichier montré dans url: 111111, 222222, 333333

Alors j'écris une correspondance dans les itinéraires:

GET  /images/$id<\w+>.*          controllers.Images.showImage(id)

Dans le $id<\w+>.* partiel, id est correspondant à l'identifiant et .* correspondre au suffixe qui sera ignoré.

Mais la syntaxe est incorrecte, le message d'erreur est:

Identifier expected

Comment résoudre ce problème?

Était-ce utile?

La solution

Il est pas possible de le faire avec le jeu 2. Pour contourner ce problème, vous pouvez traiter votre argument dans l'action du contrôleur:

GET    /images/:id       controllers.Images.showImage(id)
def showImage(idWithExt: String) = Action {
  val id = idWithExt.takeWhile(_ != '.')
  ...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top