Emi*_*nov 2 scala jetty embedded-jetty
我在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()
}
}
Run Code Online (Sandbox Code Playgroud)
该文件位于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)
Run Code Online (Sandbox Code Playgroud)
为什么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, "/")
}
}
Run Code Online (Sandbox Code Playgroud)
更新:使用a ResourceHandler会使css以正确的内容类型提供.但是,该应用程序不起作用:(
CSS文件通常由org.eclipse.jetty.servlet.DefaultServlet.提供.
哪个etc/webdefault.xml在分发中的文件中声明.
由于您使用的是嵌入模式,因此您需要通过使用文件路径调用WebAppContext.setDefaultsDescriptor(String)来手动提供此模式etc/webdefault.xml.
最后,mime类型本身由DefaultServletvia mime.properties文件加载,该文件由Jetty通过调用加载Classloader.getResource("/org/eclipse/jetty/http/mime.properties").
注意:该mime.properties文件位于jetty-http-8.1.10.v20130312.jar文件中.
| 归档时间: |
|
| 查看次数: |
5027 次 |
| 最近记录: |