简体   繁体   English

Vert.x文件根与Handlebars模板位置

[英]Vert.x file root versus Handlebars templates location

I'm constantly struggling with NoSuchFileException while trying to reach templates for HandleBars template engine for Vertx. 在尝试获取Vertx的HandleBars模板引擎的模板时,我一直在与NoSuchFileException斗争。 Personally i think that ether Vertx file system root is inconsistent or I'm missing something, code snippet is following: 我个人认为以太Vertx文件系统根目录不一致或缺少某些内容,代码片段如下:

    String templateLocation = "templates"+File.separator+"index.hbs";
    fs = vertx.fileSystem();
    fs.exists(templateLocation, existHandler -> {
        if(existHandler.succeeded() && existHandler.result() == true){
            engine.render(context,templateLocation, renderResult -> {
                if (renderResult.succeeded()) {
                    context.request().response().putHeader("Content-Type", "text/html");
                    context.response().end(renderResult.result());
                } else {
                    context.fail(renderResult.cause());
                }
            });

Firs, I'm confirming via exist, does directory and template file exist. 首先,我通过目录确认,目录和模板文件是否存在。 If yes, them start render action on same directory, I'm falling into: 如果是,它们将在同一目录上开始渲染操作,我属于:

java.nio.file.NoSuchFileException: \\emplates\\index.hbs

Event though FileSystem claims directory exist. 尽管FileSystem声明目录存在,但事件。 Where do HandleBars expect then to find it's templates? HandleBars期望在哪里找到它的模板? I'm already copy/paste folder templates/index.hbs to all possible locations: 我已经将文件夹templates/index.hbs复制/粘贴到所有可能的位置:

  • Project root 项目根目录
  • src/resources SRC /资源
  • directory where java main is executed 执行java main的目录

all with no success... 一切都没有成功...

Also please notice missing t in exception, is not a typo, looks like something in the stack is not handling very well paths 另外请注意,异常中缺少t ,不是错字,看起来像堆栈中的某些东西不能很好地处理路径

You're trying to do that the wrong way. 您正在尝试以错误的方式进行操作。 Vertx should do that for you: Vertx应该为您做到这一点:

TemplateEngine engine = HandlebarsTemplateEngine.create();
TemplateHandler handler = TemplateHandler.create(engine);

router.get("/*").handler(handler);

http://vertx.io/docs/vertx-web/java/#_handlebars_template_engine http://vertx.io/docs/vertx-web/java/#_handlebars_template_engine

This will render any template under resources/templates 这将在resources/templates下呈现任何resources/templates

If you still want to call .render by yourself for some reason, you can do it: 如果由于某些原因您仍然想自己调用.render,可以执行以下操作:

router.get("/").handler(ctx -> {
    engine.render(ctx, "templates/index.hbs", res -> {
        if (res.succeeded()) {
            ctx.response().end(res.result());
        }
    });
});

Again, this will look for your templates under /resources folder 同样,这将在/resources文件夹下查找您的模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM