为什么Jetty使用文本/html内容类型为CSS提供服务 [英] Why is Jetty serving css with text/html content type

查看:94
本文介绍了为什么Jetty使用文本/html内容类型为CSS提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Scalatra应用程序中使用嵌入式Jetty服务器.问题在于它为内容类型为text/htmlcss文件提供服务:

I'm using an embedded Jetty server in a Scalatra app. The issue is that it serves css files with text/html content type:

这是主要方法:

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文件?

Why is Jetty thinking it's an html file?

为完整性起见,这里是ScalatraBootstrap类:

Here is the ScalatraBootstrap class for completeness:

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, "/")
  }
}


更新:使用ResourceHandler可使CSS以正确的内容类型提供.但是,该应用程序无法运行:(


Update: Using a ResourceHandler causes the css to be served with correct content type. However, the app doesn't work :(

推荐答案

CSS文件通常从org.eclipse.jetty.servlet.DefaultServlet提供.

The CSS file is typically served from the org.eclipse.jetty.servlet.DefaultServlet.

在发行版的etc/webdefault.xml文件中声明了哪个.

Which is declared in the etc/webdefault.xml file in the distribution.

由于使用的是嵌入式模式,因此您需要通过调用WebAppContext.setDefaultsDescriptor(String),其中包含您的etc/webdefault.xml文件的路径.

Since you are using embedded mode, you'll want to provide this manually by calling WebAppContext.setDefaultsDescriptor(String) with the path to your etc/webdefault.xml file.

最后,DefaultServlet通过.

注意:mime.properties文件位于jetty-http-8.1.10.v20130312.jar文件中.

Note: the mime.properties file is found in the jetty-http-8.1.10.v20130312.jar file.

这篇关于为什么Jetty使用文本/html内容类型为CSS提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆