java Vertx-web:在哪里放置 webroot 文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31778971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vertx-web: where do I place webroot folder?
提问by Dave Taubler
This should be an easy one, but it's not been so far. I've been working with vert.x 2
for a bit and switched to vert.x 3
recently. I thought I'd try a simple vertx-web example but can't get past a simple serving up of static files.
这应该很容易,但到目前为止还没有。我已经工作vert.x 2
了一段时间,vert.x 3
最近切换到了。我想我会尝试一个简单的 vertx-web 示例,但无法通过简单的静态文件服务。
My server class contains the following snippets:
我的服务器类包含以下片段:
HttpServer server = vertx.createHttpServer();
Router router = ...;
router.route("/static/*").handler(StaticHandler.create().setCachingEnabled(false));
server.requestHandler(router::accept).listen(ctx.port);
I'm using Eclipse, but have also been trying running vertx
from the the command line. I'm also using Maven. I have three webroot folders, and vert.x
can find none of them:
我正在使用 Eclipse,但也一直在尝试vertx
从命令行运行。我也在使用 Maven。我有三个 webroot 文件夹,vert.x
但找不到它们:
myproject/webroot
myproject/src/main/resources/webroot
myproject/src/main/java/webroot
Each of those 'webroot's contains an index.html
, and a css/base.css
file.
每个 'webroot's 都包含一个index.html
, 和一个css/base.css
文件。
The first one is in my project's root folder. The second is in the Maven resources folder, and the third should be flat-out on my classpath
. In my Eclipse run config, I added myproject/src/main/resources/webrootto the classpath, and I made sure my working directory was set to 'myproject'. When running from the command line, I'm in the myprojectdirectory, and my script looks like this:
第一个在我的项目的根文件夹中。第二个在 Maven 资源文件夹中,第三个应该在我的classpath
. 在我的 Eclipse 运行配置中,我将myproject/src/main/resources/webroot 添加到类路径,并确保我的工作目录设置为“myproject”。从命令行运行时,我在myproject目录中,我的脚本如下所示:
JAVA_OPTS="-Dmyproject.port=8099" CLASSPATH="src/main/java:src/main/resources:target/dependencies/*:target/classes" vertx run com.my.MyProject
No matter what, I always get 404swhen I try any of these URLs:
无论如何,当我尝试这些 URL 中的任何一个时,我总是得到404:
http://localhost:8099
http://localhost:8099/
http://localhost:8099/index.html
http://localhost:8099/static/css/base.css
Anything else I need to be doing?
我还有什么需要做的吗?
回答by Hoobajoob
The solution I found depends on the answer to the questions: where is the static content going to be at runtime, and from where would you like vertx to serve it?
我找到的解决方案取决于问题的答案:静态内容在运行时会在哪里,以及您希望 vertx 从哪里提供它?
In my case, the installed file system would be where the static content was located (not in a jar file), and I wanted vertx to serve it from that location so that it could be updated live. So, I disabled classpath resolving in the StaticHandler by setting the JVM system property vertx.disableFileCPResolving
to true
at vertx startup.
就我而言,安装的文件系统将是静态内容所在的位置(不在 jar 文件中),我希望 vertx 从该位置提供它,以便它可以实时更新。所以,我禁用类路径中StaticHandler由JVM系统属性设置解析vertx.disableFileCPResolving
到true
在vertx启动。
Then I placed the webroot
folder under the directory from which the jvm is started. In my case, I'm using scripts that guarantee the jvm's cwd
is always <app-root>/bin
, so dropping the content in <app-root>/bin/webroot
was sufficient. If you can't make guarantees about where the jvm will be started from, it might be tougher, because you may need to pass an absolute path to StaticHandler.webroot() pointing to this fixed location, but I think there is an open issue regarding support for this (see here).
然后我把webroot
文件夹放在启动jvm的目录下。就我而言,我使用的脚本保证 jvm 的cwd
始终为<app-root>/bin
,因此将内容放入<app-root>/bin/webroot
就足够了。如果您不能保证 jvm 将从哪里开始,那可能会更困难,因为您可能需要将绝对路径传递给指向此固定位置的 StaticHandler.webroot(),但我认为存在一个未解决的问题关于对此的支持(请参阅此处)。
If the static content is going to be packaged into a jar, it's a little simpler. You can add 'webroot' as a resource in the jar and place all the content of interest in there. In this case you don't want to disable classpath resolving, so either set vertx.disableFileCPResolving
to false
, or don't set it at all. Now when you run vertx
, it will find webroot
in the jar file and extract its contents to <cwd>/.vertx/.file-cache-<guid>
(where cwd
is wherever you started the jvm from), and serve the contents from there. Note that this isn't viable if you intend to be able to do live updates to the content, because any changes to the files under that directory will get lost when vertx shuts down, because vertx-web
will delete that directory. On restart, it will retrieve the original files from the jar resource again, and changes will be lost.
如果静态内容要打包成jar,那就简单一点。您可以将“webroot”作为资源添加到 jar 中,并将所有感兴趣的内容放入其中。在这种情况下,您不想禁用类路径解析,因此要么设置vertx.disableFileCPResolving
为false
,要么根本不设置它。现在,当您运行 时vertx
,它将webroot
在 jar 文件中找到并将其内容提取到<cwd>/.vertx/.file-cache-<guid>
(cwd
您从何处启动 jvm 的位置),并从那里提供内容。请注意,如果您打算对内容进行实时更新,这是不可行的,因为当 vertx 关闭时,对该目录下文件的任何更改都将丢失,因为vertx-web
将删除该目录。重新启动时,它将再次从 jar 资源中检索原始文件,更改将丢失。
Anyway, hope this helps.
无论如何,希望这会有所帮助。
回答by geg
Execute the following code at any breakpoint and it'll tell you the root directory from which it will resolve your resource request:
在任何断点处执行以下代码,它会告诉您它将解析您的资源请求的根目录:
new File("").getCanonicalPath()
Request: localhost:8080/static/script.js
请求:本地主机:8080/static/script.js
Handler:
处理程序:
router.route("/static/*").handler(StaticHandler.create("webroot"));
The file should be in MODULE_ROOT/webroot/script.js
该文件应该在 MODULE_ROOT/webroot/script.js
Debug all the way into the StaticHandlerImpl#sendStatic
method to see how your files are being resolved, and use the debug expression window liberally to interrogate the file system.
一直调试到StaticHandlerImpl#sendStatic
方法中以查看您的文件是如何被解析的,并自由地使用调试表达式窗口来询问文件系统。
回答by devsathish
The default classpath setting worked for me. I used the following hierarchy:
默认的类路径设置对我有用。我使用了以下层次结构:
Keep the static file in main/resources/webroot
directory
将静态文件保存在main/resources/webroot
目录中
<AppRoot>/src/main/resources/webroot/static/index.html
And initialize vertx as follows:
并按如下方式初始化 vertx:
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
Now you can access it from this URL: http://localhost:8080/static/
现在你可以从这个 URL 访问它: http://localhost:8080/static/
Refer vertx-web documentationfor more info.
有关更多信息,请参阅vertx-web 文档。
回答by Dave Taubler
Alright, I'll somewhat answer my own question. First, I should point out that I was keying off of the static-content parts of the vertx-web docs (http://vertx.io/docs/vertx-web/js/#_serving_static_resources):
好吧,我会稍微回答一下我自己的问题。首先,我应该指出我正在关闭 vertx-web 文档(http://vertx.io/docs/vertx-web/js/#_serving_static_resources)的静态内容部分:
Any requests to paths handled by the static handler will result in files being served from a directory on the file system or from the classpath. The default static file directory is webroot but this can be configured.
In the following example all requests to paths starting with /static/ will get served from the directory webroot:
对静态处理程序处理的路径的任何请求都将导致从文件系统上的目录或类路径中提供文件。默认静态文件目录是 webroot 但这可以配置。
在以下示例中,所有对以 /static/ 开头的路径的请求都将从目录 webroot 获得服务:
var StaticHandler = require("vertx-web-js/static_handler");
router.route("/static/*").handler(StaticHandler.create().handle);
and
和
Any requests to the root path / will cause the index page to be served. By default the index page is index.html. This can be configured with setIndexPage.
对根路径 / 的任何请求都将导致提供索引页面。默认情况下,索引页面是 index.html。这可以通过 setIndexPage 进行配置。
It seemed to me that if I didn't explicitly define a handler for "/", then index.html would implicitly be served. And it also seemed that just creating a StaticHandler and adding it to the router would suffice for CSS/JS/IMG resources. I think my assumptions were incorrect.
在我看来,如果我没有为“/”明确定义处理程序,那么 index.html 将被隐式提供。而且似乎只需创建一个 StaticHandler 并将其添加到路由器就足以处理 CSS/JS/IMG 资源。我认为我的假设是不正确的。
So I added the following, which seem to be what was needed:
所以我添加了以下内容,这似乎是所需要的:
first, I told the StaticHandler explicitly to look for a "webroot" folder:
首先,我明确告诉 StaticHandler 查找“webroot”文件夹:
router.route("/static/*").handler(StaticHandler.create("webroot").setCachingEnabled(false));
then, I explicitly added a route to my router to handle requests to "/":
然后,我明确地向我的路由器添加了一个路由来处理对“/”的请求:
router.route("/").handler(ctx -> {
Logger.log("Got an HTTP request to /");
ctx.response().sendFile("webroot/index.html").end();
});
回答by Kishore Tulsiani
this solution worked for me, to add more details here is the folder structure for me
这个解决方案对我有用,在这里添加更多细节是我的文件夹结构
src
main
java
com
-- java files with vertx
resources
webroot
html
js
回答by Anders B
Directory
目录
<AppRoot>/webroot/index.html
Vertx
顶点
router.route("/app/*").handler(StaticHandler.create("webroot").setCachingEnabled(false));
URL
网址
http://127.0.0.1/app/index.html
Add the following Java VM options to enable hot reload
添加以下 Java VM 选项以启用热重载
-Dvertx.disableFileCPResolving=true
回答by niro
Static files kept in /src/main/resources/assets/
静态文件保存在 /src/main/resources/assets/
ex: index.html
例如:index.html
Router router = Router.router(vertx);
router.route("/files/*").handler(StaticHandler.create("assets"));
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
Now you can access it from http://localhost:8080/files/index.html
现在你可以从http://localhost:8080/files/index.html访问它
If you look at router.route("/files/*").handler(StaticHandler.create("assets"));
如果你看 router.route("/files/*").handler(StaticHandler.create("assets"));
When a request matches /files/
it calls the StaticHandler which specifies assets
folder as the web-root
当请求匹配时,/files/
它调用 StaticHandler 指定assets
文件夹作为web-root
回答by kva1966
I believe people are getting tripped up by the examples in the documentation for serving static resources at http://vertx.io/docs/vertx-web/kotlin/#_serving_static_resources-- some of the answers even go so far as to change the route path, which is not what is desired!
我相信人们被http://vertx.io/docs/vertx-web/kotlin/#_serving_static_resources 上提供静态资源的文档中的示例绊倒了——有些答案甚至改变了路由路径,这不是我们想要的!
I have raised https://github.com/vert-x3/issues/issues/265with details.
我已经提出了https://github.com/vert-x3/issues/issues/265的详细信息。
Among others, OP tried:
其中,OP 尝试过:
http://localhost:8099/static/css/base.css
That should have worked, the rest of the paths were not valid, and src/main/resources/webroot
is a good place for packaged-up static resources.
这应该有效,其余路径无效,并且src/main/resources/webroot
是打包静态资源的好地方。