LoginSignup
5
5

More than 5 years have passed since last update.

sprayで静的ファイルを扱う

Posted at
  • 画像
  • css
  • js
  • web Font

など(以下resources)をどうやって扱うかという話。

日本語の記述が全く見当たらなかったのでメモ代わりに。

ディレクトリ構成

src/main/resourcesがresourcesを扱うためのルートディレクトリになる。

メソッド

getFromResource

getFromResource
パスとファイル名を直接指定する場合に用いる

src/main/resources/hoge.txtを表示したい場合

get {
  path("hoge") {
    getFromResource("hoge.txt")
  }
}

getFromResourceDirectory

getFromResourceDirectory
pathPrefixと組み合わせて用いる

src/main/resources/css/*.cssの読み込み

get {
  pathPrefix("css") {
    getFromResourceDirectory("css")
  }
}

こうしておくと、html側で

<link href="css/foo.css" rel="stylesheet">
<link href="css/bar.css" rel="stylesheet">

このように、hrefに指定するパスをcssから始めれば読み込むことができる

要調査: 任意のディレクトリをresourcesのルートとして扱う方法

build.sbtunmanagedResourceDirectoriesの指定を入れたら任意のディレクトリを指定できるっぽいけど上手く行かなかった

参考: spray-twirl-bootstrap

build.sbt
unmanagedResourceDirectories in Compile <++= baseDirectory { base =>
    Seq( base / "src/main/webapp" )
}
5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5