gpt4 book ai didi

java - 如何从 Freemarker 模板中获取特定的 map 值

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:01 26 4
gpt4 key购买 nike

我需要传递一个 Map<String,String>到 Freemarker 模板中,然后在模板内部,cherry pick 我从注入(inject)的映射中提取的值。所以像这样:

// This map will have a 'fizz' key with a String value of 'buzz'.
Map<String,String> mymap = getSomehow();

Configuration cfg = new Configuration();
Template template = cfg.getTemplate("mytempl.ftl");
StringWriter sw = new StringWriter();

template.process(mymap, sw);

然后,模板(mytempl.ftl):

<h1>${mymap[fizz]}</h1>

但是当我运行它时,我得到:

FreeMarker template error: The following has evaluated to null or missing: ==> mymap

有什么地方会出错吗?

最佳答案

process 的第一个参数方法是模板的“上下文”。因此,要从 Map 中选择特定值,您可以使用 <h1>${fizz}</h1> .

关于java - 如何从 Freemarker 模板中获取特定的 map 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com