我在Scala上實現一個簡單的Web服務器與AKK http路由DSL等。我有(例如):Akka HTTP響應HTML字符串與循環的CSS或圖像鏈接
val route = get {
path("test") {
complete((new ViewTemplate).response)
}
}
凡ViewTemplateis類誰讀一些HTML模板,一些價值注入它可能使一些轉換和收益作爲HttpResponse對象...
class ViewTemplate(val filename: String = "test.html") {
import scala.io.Source
private val template = Source.fromResource(filename)
override def toString: String = template.mkString
def entity: ResponseEntity = HttpEntity(ContentTypes.`text/html(UTF-8)`, toString)
def response: HttpResponse = HttpResponse(entity = entity)
}
這一切的作品直到我加入
<link rel="stylesheet" href="style.css"/>
into test.html的頭部。瀏覽器完全忽略了這個參考。與圖像和a的東西相同的情況。我想,像遊戲噴劑這樣的事情處理這種情況很好,我正在發明另一輛自行車,但我只是在尋找根。那麼你有什麼建議?
這個問題已經關閉。 getFromResourceDirectory(「」)發揮了魔力。 –