Route to static file in Play! 2.0

后端 未结 9 1749
甜味超标
甜味超标 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:36

    IIRC, change

    <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
    

    To

    <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/", "main.css")">
    

    I am talking about your third attempt

    Also, watch out for extra /

    EDIT

    GET /assets/main.css    controllers.Assets.at(path="/public", file="/stylesheets/main.css")
    

    Assuming your resource is at /public/stylesheets/main.css

    0 讨论(0)
  • 2021-01-30 14:36

    I am not totally sure if this is correct, but this is what we are using to map a public folder containing our images, javascripts... etc..

    # Map static resources from the /public folder to the /assets URL path
    GET     /assets/*file               controllers.Assets.at(path="/public", file)
    
    0 讨论(0)
  • 2021-01-30 14:36

    This ability still haven't been added, as I know. But if someone needs answer, as an option, helper controller can be created for these purpose:

    object StaticFile extends Controller {
    
      def html(file: String) = Action {
        var f = new File(file)
    
        if (f.exists())
          Ok(scala.io.Source.fromFile(f.getCanonicalPath()).mkString).as("text/html");
        else
          NotFound
      }
    
    }
    

    and then in routes config

    GET     /               controllers.StaticFile.html(file = "public/index.html")
    
    0 讨论(0)
提交回复
热议问题