micronaut - 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 助手?
解决方案
当前 Micronaut 1.0 GA 版本中没有注册 Handlebars 助手的配置。但是,您可以应用一个简单的解决方法来克服此限制。要使助手注册成为可能,您必须访问io.micronaut.views.handlebars.HandlebarsViewsRenderer
class 及其内部属性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.加载HandlebarsCustomConfig
bean
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();
}
}
}
与以前的解决方案相比,它具有一些优点:
- 您不必在
io.micronaut.views.handlebars
包中创建它。 - 您不必在
main
方法中获取 bean 即可正确初始化它。
推荐阅读
- outlook - Outlook add-in publish on a Cpanel
- haskell - 如何使用我在haskell中定义的另一个函数创建我自己的地图
- c++ - C++ Xtensor 增加浮点有效数
- javascript - 如何在 Google 时间线图表上显示截断的文本?
- java - 在春季的另一个控制器建议中使用全局模型属性
- python - 如何将 NLTK 标签转换为 wordnet 标签
- r - 一元运算符的参数无效(在字符列上)
- node.js - Sequelize:通过另一个表与自我建立多对多关系?
- html - 仅 CSS 的图像工具提示
- c++ - 迭代二维结构时,迭代总长度更快还是使用嵌套循环迭代行和列?