指定 JAR 的 ResourceBase “resources/webapp"的正确 URL 是什么?嵌入式 Jetty 的文件夹? [英] What is correct URL to specify ResourceBase of JAR "resources/webapp" folder for embedded Jetty?
问题描述
我们想要一个简单的嵌入式 Jetty servelet,其 Web 资源位于 JAR 文件的资源文件夹中.我们在 JAR 中有一些属性文件并使用资源路径加载它们.我们希望将 Jetty Resource Base 指定为:
We want a simple embedded Jetty servelet with the web resources inside a JAR-file's resources folder. We have some properties files in the JAR and load them using a resources path. We want to specify the Jetty Resource Base to be:
- 资源/webapp
- set: resource_handler.setResourceBase( "webapp" )
- 通过正确的 URL 指向 JAR 文件.
- resources/webapp
- set: resource_handler.setResourceBase( "webapp" )
- Via the correct URL to point to that resource in the JAR file.
JAR 文件中的文件夹.这是一个简单的 JAR 文件(不是 WAR,没有框架,没有 Spring,就像我们可能的普通).初始测试继续抛出类似以下字符串的异常:
Folder in the JAR file. This is a bare bones JAR file (not WAR, no frameworks, without Spring, as vanilla as we may). Initial tests continue to throw exceptions for something like the following strings:
webPath = "jar:file:!/webapp"; //.... runs the Jetty server ... resource_handler.setResourceBase( webPath );
虽然服务器好像在运行,结果还是找不到我的index.html.(更新:) 这个例子只是取自 Jetty "嵌入式文件服务器" 示例.在这种情况下,要求 Jetty Resource Base 映射到 JAR 文件(完整 URL):
Although the server seems to run, the result fails to find my index.html. (Update:) This example is just taking from the Jetty "Embedded File Server" example. In this case, the requirement is for the Jetty Resource Base to map to the JAR file (full-URL):
- "jar:file:!/webapp/index.html",
如下:
- resource_handler.setResourceBase("jar:file:!/webapp");
代替给出的例子:
- resource_handler.setResourceBase(".");
我们希望将浏览器 URL 映射为:
And we want this to map the browser URL as:
- 本地主机:8080/index.html
- ...给予...
对比以下适用于配置文件的 JAR 路径.问题:URL Jetty 资源库可以提供我的 Index.html 文件吗?
For contrast the JAR path that work for config files below. The question: what should the URL be so Jetty resource base can serve-up my Index.html file?
- 资源/
- 配置/
- display.properties
文件是:/config/display.properties",它使用加载资源操作在相同的项目代码中工作.布局是这样的:
file is: "/config/display.properties" and this works in the same project code using a load resources operation. The layout is like this:
app.jar +----- com / | +--- ( classes ... ) | +----- config / | | | +--- display.properties | +----- webapp / | +--- index.html
给出总体思路.
类似问题:
推荐答案
我有一个可行的解决方案 - 我发布的解决方法是希望这种方法能够激发正确的方法.我仍然相信应该有一种方法来指定 JAR 内的文件夹,相对于 JAR.
I have a working solution - Work-around that I'm posting in the hope that this approach will inspire correct method. I still believe there ought to be a way to specify a folder inside the JAR, relative to the JAR.
无论如何这个方法是有效的.我用它来从 JAR 中提供静态 Web 内容.本质上,我让 Java 解析正在运行的 JAR 资源的绝对路径并将该路径名传递给 Jetty.当我这样做时,Jetty 会显示我的helloWorld.html",欢迎文件.
Anyway this method works. I used it to server static web content from within the JAR. Essentially I have Java resolve the absolute path to the running JAR resource and pass that path name to Jetty. When I do that Jetty displays my "helloWorld.html", welcome file.
String baseStr = "/webapp"; //... contains: helloWorld.html, login.html, etc. and folder: other/xxx.html URL baseUrl = SplitFileServerRunner.class.getResource( baseStr ); String basePath = baseUrl.toExternalForm(); .... resource_handler.setDirectoriesListed(true); //... just for testing resource_handler.setWelcomeFiles(new String[]{ "helloWorld.html" }); resource_handler.setResourceBase( basePath ); LOG.info("serving: " + resource_handler.getBaseResource());
在欢迎文件中,我放置了特定文本来标识文件的来源(在资源文件夹中).在浏览器中:
In the welcome file, I have put specific text to identify the file's origin (in the resources folder). In the browser:
- 本地主机:8080
提供 helloWorld.html 文件.
Serves the helloWorld.html file.
- 本地主机:8080/其他
显示 JAR 文件中 jar:/webapp/other/ 目录的目录列表.这依赖于不在服务器运行时更改 JAR.
Shows a directory listing of the jar:/webapp/other/ directory inside the JAR file. This relies on not changing the JAR while the server is running.
在 Linux 上,如果有人 cp-s 在正在运行的 JAR 之上创建一个新的 jarfile,Jetty 会给出:
On Linux if someone cp-s a new jarfile on-top of the running JAR, Jetty gives:
HTTP ERROR: 500 Problem accessing /. Reason: java.lang.NullPointerException
而且您无法再访问页面.这是出乎意料的(显然 JAR 保持打开状态).好消息是,如果您 mv-s jarfile:
And you can't access pages any more. That was unexpected (evidently the JAR is kept open). The good news is that if you mv-s the jarfile:
- mv fileserver.jar fileserverXX.jar
Jetty 愉快地继续从(重命名的)fileserverXX.jar 内容提供服务.我可以很高兴.但是我仍然想知道匹配绝对文件名的等效相对路径.
Jetty happily continues serving from the (renamed) fileserverXX.jar content. I can be happy with that. However I'd still like to know the equivalent relative path to match the absolute file name.
这篇关于指定 JAR 的 ResourceBase “resources/webapp"的正确 URL 是什么?嵌入式 Jetty 的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 配置/