Freemarker错误预期哈希? [英] Freemarker Error Expected hash?

查看:138
本文介绍了Freemarker错误预期哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将即时时间转换为日期,但出现此错误:

I want to transform a Instant time to Date but I'm getting this error:

freemarker.template.TemplateException:预期的哈希. newDate改为评估为freemarker.template.SimpleDate

freemarker.template.TemplateException: Expected hash. newDate evaluated instead to freemarker.template.SimpleDate

我正在Java上执行此操作:

I'm doing this on Java:

Date newDate = new Date();
Instant instant =  Instant.now();

webContext.put("newDate",new Date());
webContext.put("instant",instant);

我正在Freemarker上执行此操作

And I'm doing this on Freemarker:

[#assign dateFormated = newDate.getAsDate().from(instant.ofEpochSecond(data.time.seconds))/]

谢谢

推荐答案

FreeMarker模板通常不公开Java API,也不允许您按名称访问Java类.我的意思是,在某些情况下确实如此,但总的来说不像newDate在FreeMarker中没有子变量(如getAsDate).有一些实用程序可用来公开类的静态方法,例如:

FreeMarker templates don't in general expose Java API-s, or allow you to access Java classes by name. I mean, in some cases it does, but not in general like newDate has no subvariables (like getAsDate) in FreeMarker. There are utilities with which you can expose the static methods of classes, like:

TemplateHashModel staticModels
        = ((BeansWrapper) configuration.getObjectWrapper())
          .getStaticModels();
webContext.put("Date", staticModels.get("java.util.Date"));
webContext.put("Instant", staticModels.get("java.time.Instant"));

其中,configuration是您的freemarker.template.Configuration单身人士.实际上,您可以在配置FreeMarker的位置使用Configuration.setSharedVariableDateInstant添加到该单例中.

where configuration is your freemarker.template.Configuration singleton. Actually, you can add Date and Instant to that singleton with Configuration.setSharedVariable, once where you configure FreeMarker.

然后,您可以将Date.from(Instant.now())写入模板,因为现在有一个DateInstant变量,并且您明确地告诉FreeMarker公开了它们的静态方法.

And then, you can write Date.from(Instant.now()) into a template, because now there's a Date and and Instant variable, and you have specifically told FreeMarker to expose thier static methods.

这篇关于Freemarker错误预期哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆