本文整理汇总了Java中com.github.mustachejava.reflect.ReflectionObjectHandler类的典型用法代码示例。如果您正苦于以下问题:Java ReflectionObjectHandler类的具体用法?Java ReflectionObjectHandler怎么用?Java ReflectionObjectHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ReflectionObjectHandler类属于com.github.mustachejava.reflect包,在下文中一共展示了ReflectionObjectHandler类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: renderMustache
import com.github.mustachejava.reflect.ReflectionObjectHandler; //导入依赖的package包/类
/**
* Renders a given Mustache template using the provided value map, returning any template parameters which weren't
* present in the map.
*
* @param templateContent String representation of template
* @param values Map of values to be inserted into the template
* @param missingValues List where missing value entries will be added for any template params in
* {@code templateContent} which are not found in {@code values}
* @return Rendered Mustache template String
*/
public static String renderMustache(
String templateName,
String templateContent,
Map<String, String> values,
final List<MissingValue> missingValues) {
StringWriter writer = new StringWriter();
DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory();
mustacheFactory.setObjectHandler(new ReflectionObjectHandler() {
@Override
public Binding createBinding(String name, final TemplateContext tc, Code code) {
return new MissingValueBinding(this, name, tc, code, missingValues);
}
});
Map<String, Object> objEnv = new HashMap<>();
for (Map.Entry<String, String> entry : values.entrySet()) {
if (StringUtils.equalsIgnoreCase(entry.getValue(), "false") ||
StringUtils.equalsIgnoreCase(entry.getValue(), "true")) {
objEnv.put(entry.getKey(), Boolean.valueOf(entry.getValue()));
} else {
objEnv.put(entry.getKey(), entry.getValue());
}
}
mustacheFactory
.compile(new StringReader(templateContent), templateName)
.execute(writer, objEnv);
return writer.toString();
}
示例2: testRendering
import com.github.mustachejava.reflect.ReflectionObjectHandler; //导入依赖的package包/类
@Test
public void testRendering() {
My.templatesPath("view-rendering");
MustacheJavaViewResolver viewResolver = Integrate.mustacheJavaViewResolver();
viewResolver.setCustomizer(compiler -> {
compiler.setObjectHandler(new ReflectionObjectHandler() {
@Override
public String stringify(Object object) {
return "[" + object + "]";
}
});
return compiler;
});
My.viewResolver(viewResolver);
On.get("/").view("mtmpl").mvc((req, resp) -> {
resp.model("y", "bar");
return U.map("x", "foo");
});
getReq("/");
}