为什么Jetty使用文本/html内容类型为CSS提供服务 [英] Why is Jetty serving css with text/html content type
问题描述
我正在Scalatra应用程序中使用嵌入式Jetty服务器.问题在于它为内容类型为text/html
的css
文件提供服务:
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屋!