我有以下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]

08-03 23:07