首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Micronaut /车把助手

Micronaut /车把助手
EN

Stack Overflow用户
提问于 2018-11-07 21:59:29
回答 1查看 456关注 0票数 1

我想知道我应该在哪里(或如何)声明我的micronaut项目中的把手的帮助器?

我尝试了以下方法:

代码语言:javascript
运行
复制
public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
        Handlebars handlebars = new Handlebars();
        handlebars.registerHelpers(HelperSource.class);
    }
}

当然,没有效果。如何在Micronaut应用程序中成功注册Handlebars?

EN

回答 1

Stack Overflow用户

发布于 2018-11-07 23:40:15

在当前的Micronaut 1.0 GA版本中,没有注册Handlebar辅助对象的配置。但是,您可以应用一种简单的解决方法来克服此限制。要使帮助器注册成为可能,您必须访问io.micronaut.views.handlebars.HandlebarsViewsRenderer类及其内部属性handlebars。好消息是这个属性有一个protected作用域-这意味着我们可以在源代码中的同一个包中创建另一个bean,我们可以注入HandlebarsViewsRenderer并访问HandlebarsViewsRenderer.handlebars字段。有了这个字段的访问权,我们就可以执行handlebars.registerHelpers(...)方法了。

您可以简单地执行以下步骤:

1.添加Handlebars.java依赖

代码语言:javascript
运行
复制
compile "com.github.jknack:handlebars:4.1.0"

将其添加到编译作用域中很重要,因为运行时作用域不允许访问HandlebarsViewsRenderer.handlebars对象。

2.创建io.micronaut.views.handlebars.HandlebarsCustomConfig

src/main/java/io/micronaut/views/handlebars/HandlebarsCustomConfig.java

代码语言:javascript
运行
复制
package io.micronaut.views.handlebars;

import javax.inject.Singleton;
import java.util.Date;

@Singleton
public final class HandlebarsCustomConfig {

    public HandlebarsCustomConfig(HandlebarsViewsRenderer renderer) {
        renderer.handlebars.registerHelpers(new HelperSource());
    }

    static public class HelperSource {
        public static String now() {
            return new Date().toString();
        }
    }
}

在这个类中,我创建了一个简单的HelperSource类,它公开了一个名为{{now}}的帮助器。

3.加载HandlebarsCustomConfig bean

代码语言:javascript
运行
复制
package com.github.wololock.micronaut;

import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.Micronaut;
import io.micronaut.views.handlebars.HandlebarsCustomConfig;

public class Application {

    public static void main(String[] args) {
        final ApplicationContext ctx = Micronaut.run(Application.class);
        ctx.getBean(HandlebarsCustomConfig.class);
    }
}

这一步至关重要。我们需要加载bean,否则Micronaut不会创建它的实例,我们的helpers注册也不会发生。

4.创建视图

src/main/resources/views/home.hbs

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>Hello, world!</h1>
    <p>Now is {{now}}</p>
</body>
</html>

5.运行应用程序并查看结果

@Replaces替代方案

您可以使用Micronauts @Replaces注释将HandlebarsViewsRenderer替换为自定义实现。

代码语言:javascript
运行
复制
import io.micronaut.context.annotation.Replaces;
import io.micronaut.core.io.scan.ClassPathResourceLoader;
import io.micronaut.views.ViewsConfiguration;

import javax.inject.Singleton;
import java.util.Date;

@Singleton
@Replaces(HandlebarsViewsRenderer.class)
public final class CustomHandlebarsViewsRenderer extends HandlebarsViewsRenderer {

    public CustomHandlebarsViewsRenderer(ViewsConfiguration viewsConfiguration, 
                                  ClassPathResourceLoader resourceLoader, 
                                  HandlebarsViewsRendererConfiguration handlebarsViewsRendererConfiguration) {
        super(viewsConfiguration, resourceLoader, handlebarsViewsRendererConfiguration);

        this.handlebars.registerHelpers(new HelperSource());
    }

    static public class HelperSource {
        public static String now() {
            return new Date().toString();
        }
    }
}

与以前的解决方案相比,它有几个优势:

  1. 您不必在io.micronaut.views.handlebars包中创建它。
  2. 您不必在main方法中获取bean即可正确地对其进行初始化。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53190958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档