質問

In my build.sbt I have:

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

This is my 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
  }

In the notFound method I have:

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

The webservices are accessible, and work as I expect, but the static content (in this case index.html) isn't being found, when I launch my application with

java -jar myapp.jar

I have a feeling I need to write my own serveStaticResource function, but I am hoping not.

This is what I see, and the HTTP 200 is due to the 'bummer' response of not finding the file:

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

How do I configure the Jetty Launcher so it will properly find the static files?

Is there a way to tell it that src/main/webapp is a resource folder?

The other problem I have is that in the notFound function I don't see how to know which file wasn't found so I can then just call this.context.getResource("src/main/webapp" + missingfile) myself, from withing Scalatra. I think that is the root of my difficulty is that I don't now which file was not found.

役に立ちましたか?

解決

The line you had there to set the resource base is perfectly fine:

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

And the notFound method is good as well as it is, Scalatra will pick up an index.html file if you put it under the resource base:

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

In order to have an executable jar (not running from sbt), you will need to execute the jar from a folder where you still have the src/main/webapp folder and all its content relative to your execution.

Note: request.getRequestURL will give you the request URL, but I do not recommend rewriting the serveStaticResource as it is doing its job well

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

Note2: Have a look at https://github.com/softwaremill/bootzooka they separate the REST services and the UI layer (with html+angularJS) quite well.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top