我在Scalatra应用程序中使用嵌入式Jetty服务器。问题是它提供具有css
内容类型的text/html
文件:
以下是主要的方法:
package yard.web
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.webapp.WebAppContext
import org.scalatra.servlet.ScalatraListener
object JettyMain {
def main(args: Array[String]) {
val server = new Server(9080)
val context: WebAppContext = new WebAppContext("src/main/webapp", "/")
context.setServer(server)
context.setInitParameter(ScalatraListener.LifeCycleKey, "yard.web.ScalatraBootstrap")
context.addEventListener(new ScalatraListener())
server.setHandler(context)
server.start()
println("Press ENTER to stop server")
Console.readLine()
server.stop()
server.join()
}
}
该文件位于src/main/webapp/libs/bootstrap/css/bootstrap.css
,并与以下文件一起使用:
$ curl --head http://localhost:9080/libs/bootstrap/css/bootstrap.css
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Last-Modified: Sat, 06 Apr 2013 14:30:35 GMT
Content-Length: 127247
Accept-Ranges: bytes
Server: Jetty(8.1.10.v20130312)
为什么Jetty认为它是html文件?
以下是完整性的ScalatraBootstrap
类:
package yard.web
import org.scalatra.LifeCycle
import javax.servlet.ServletContext
import yard.Settings
import yard.db.Store
class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) {
val settings = Settings.default
val db = Store(settings).db
context mount (new MainServlet, "/")
}
}
Update:使用ResourceHandler
会使css具有正确的内容类型。然而,这个应用程序不起作用:
发布于 2013-04-06 15:31:20
CSS文件通常是从org.eclipse.jetty.servlet.DefaultServlet
提供的。
它在发行版的etc/webdefault.xml
文件中声明。
由于您使用的是嵌入式模式,所以您希望通过使用指向您的WebAppContext.setDefaultsDescriptor(String)文件的路径来手动调用etc/webdefault.xml
。
最后,mime类型本身由DefaultServlet
通过mime.properties
文件加载,Jetty通过调用Classloader.getResource("/org/eclipse/jetty/http/mime.properties")
加载该文件。
注意:mime.properties
文件在jetty-http-8.1.10.v20130312.jar
文件中找到。
https://stackoverflow.com/questions/15852443
复制相似问题