Java – how to pass string as input in FreeMarker?

There is already an answer to this question: > 4 load FreeMarker templates from database

However, I didn't find any way to send a string instead of a file name

Please advise

Solution

You can use stringreader:

// Get your template as a String from the DB
  String template = getTemplateFromDatabase();
  Map<String,Object> model = getModel();

  Configuration cfg = new Configuration();
  cfg.setObjectWrapper(new DefaultObjectWrapper());

  Template t = new Template("templateName",new StringReader(template),cfg);

  Writer out = new StringWriter();
  t.process(model,out);

  String transformedTemplate = out.toString();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>