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"));
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.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…