gpt4 book ai didi

java - 如何在 freemarker 中获取特定键的值

转载 作者:行者123 更新时间:2023-11-29 04:27:28 24 4
gpt4 key购买 nike

我有以下 HashMap:

 Map<String, Object> hashMap = new HashMap<>();
hashMap.put("type", "message");

我在freemarker模板中写了一个switch语句:

    <#switch hashMap.get("type")>
<#case "message">
<p>Message type</p>
<#default>
<p>Nothing</p>
</#switch>

但是当我执行代码时,出现异常:


freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134)
at freemarker.core.UnexpectedTypeException.newDesciptionBuilder(UnexpectedTypeException.java:80)
at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:43)
at freemarker.core.NonHashException.<init>(NonHashException.java:49)
at freemarker.core.Dot._eval(Dot.java:48)
at freemarker.core.Expression.eval(Expression.java:81)
at freemarker.core.MethodCall._eval(MethodCall.java:58)
at freemarker.core.Expression.eval(Expression.java:81)
at freemarker.core.EvalUtil.compare(EvalUtil.java:111)
at freemarker.core.SwitchBlock.accept(SwitchBlock.java:67)
at freemarker.core.Environment.visit(Environment.java:326)
at freemarker.core.Environment.visit(Environment.java:332)
at freemarker.core.Environment.process(Environment.java:305)
at freemarker.template.Template.process(Template.java:378)
at com.comp.www.reports.FreeMarkerReport.main(FreeMarkerReport.java:70)

任何人都可以指出我做错了什么吗?

最佳答案

代替

hashMap.get("type")

使用

hashMap[type]

关于java - 如何在 freemarker 中获取特定键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45554911/

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