Scalatra从可执行jar文件加载静态内容

时间:2013-10-25 13:18:43

标签: scalatra

在我的build.sbt中,我有:

resourceDirectory in Compile <<= baseDirectory(_ / "src/main/webapp")

这是我的JettyLauncher.main:

  def main(args: Array[String]) {
    val port = if(System.getenv("PORT") != null) System.getenv("PORT").toInt else 8080

    println(s"JettyLauncher: ${port}")
    val server = new Server(port)
    val context = new WebAppContext()
    context setContextPath "/"
    context.setResourceBase("src/main/webapp")
    context.addServlet(classOf[MyServlet], "/*")

    server.setHandler(context)

    server.start
    server.join
  }

我使用notFound方法:

notFound {
  // remove content type in case it was set through an action
  contentType = null
  serveStaticResource() getOrElse <h1>Not found. Bummer.</h1>
}

Web服务是可访问的,并按预期工作,但是当我使用

启动我的应用程序时,找不到静态内容(在本例中为index.html)
java -jar myapp.jar

我有一种感觉,我需要编写自己的serveStaticResource函数,但我希望不会。

这就是我所看到的,HTTP 200是由于没有找到文件的“无赖”反应:

09:13:44.735 [qtp874703452-16 - /index.html] DEBUG org.eclipse.jetty.server.Server - REQUEST /index.html on AsyncHttpConnection@fb4871b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=14,c=0},r=3
09:13:44.748 [qtp874703452-17] DEBUG org.eclipse.jetty.http.HttpParser - filled 0/0
09:13:44.752 [qtp874703452-16 - /index.html] DEBUG o.e.j.server.handler.ContextHandler - scope null||/index.html @ o.e.j.w.WebAppContext{/,file:/C:/Users/src/main/webapp}
09:13:44.759 [qtp874703452-16 - /index.html] DEBUG o.e.j.server.handler.ContextHandler - context=||/index.html @ o.e.j.w.WebAppContext{/,file:/C:/Users/src/main/webapp}
09:13:44.764 [qtp874703452-16 - /index.html] DEBUG org.eclipse.jetty.server.session - sessionManager=org.eclipse.jetty.server.session.HashSessionManager@5f8b459a
09:13:44.768 [qtp874703452-16 - /index.html] DEBUG org.eclipse.jetty.server.session - session=null
09:13:44.771 [qtp874703452-16 - /index.html] DEBUG o.e.jetty.servlet.ServletHandler - servlet ||/index.html -> gov.ornl.cyber.botmon.ui.BotMonServlet-1
09:13:44.774 [qtp874703452-16 - /index.html] DEBUG o.e.jetty.servlet.ServletHandler - chain=null
09:13:44.813 [qtp874703452-16 - /index.html] DEBUG org.eclipse.jetty.server.Server - RESPONSE /index.html  200 handled=true

如何配置Jetty Launcher以便正确查找静态文件?

有没有办法告诉它src / main / webapp是资源文件夹?

我遇到的另一个问题是在notFound函数中我没有看到如何知道找不到哪个文件,所以我可以自己调用this.context.getResource("src/main/webapp" + missingfile)来自Scalatra。我认为这是我困难的根源,我现在没有找到哪个文件。

1 个答案:

答案 0 :(得分:1)

您在那里设置资源库的界限非常好:

  context.setResourceBase("src/main/webapp")

notFound 方法很好,如果你将它放在资源库下,Scalatra会选择一个index.html文件:

notFound {
  contentType = null // although probably you would want to set this to 'text/html'
  serveStaticResource() getOrElse <h1>Not found. Bummer.</h1>
}

为了拥有一个可执行jar(不是从sbt运行),你需要从一个文件夹中执行jar,你仍然拥有 src / main / webapp 文件夹及其所有相关内容你的执行。

注意:request.getRequestURL将为您提供请求URL,但我不建议重写serveStaticResource,因为它正在完成其工作

 notFound {
    println("Oh yeah: " + request.getRequestURL)
}

注意2:看看https://github.com/softwaremill/bootzooka他们将REST服务和UI层(使用html + angularJS)分开。