Route to static file in Play! 2.0

后端 未结 9 1762
甜味超标
甜味超标 2021-01-30 14:05

I\'m trying to make a route to a specific static file but everything I\'m trying ends with an error.

I\'ve made 3 different attempts:

1.

GET /fil         


        
9条回答
  •  死守一世寂寞
    2021-01-30 14:33

    The solution to this that is cleanest is to create your own AssetsBuilder that will build your static page.

    Create this file in your controllers package - Static.scala

    package controllers
    
    object Static extends AssetsBuilder
    

    Then in your routes you can define your static endpoint

    GET /file   controllers.Static.at(path="/public/html", "file.html")
    

    Done. Now the file at /public/html/file.html will be served off of localhost:9000/file

    If you replace the hard code above with a more generic:

    GET /*file   controllers.Static.at(path="/public/html", *file + ".html")
    

    then /foo will serve /public/html/foo.html, /bar will serve /public/html/bar.html etc.

提交回复
热议问题