首页 > 解决方案 > Micronaut / 车把助手

问题描述

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

我尝试了以下方法:

public class Application {

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

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

标签: micronauthandlebars.java

解决方案


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

您可以简单地按照以下步骤操作:

1.添加Handlebars.java依赖

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

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.加载HandlebarsCustomConfigbean

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 不会创建它的实例,我们的助手注册也不会发生。

4. 创建视图

src/main/resources/views/home.hbs

<!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自定义实现。

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 即可正确初始化它。

推荐阅读