【发布时间】:2020-02-18 20:48:32
【问题描述】:
我有一个类似下面的java代码
public static void main(String args[]) throws Exception {
VelocityEngine engine = new VelocityEngine();
engine.init();
Template template = engine.getTemplate("userinfo.vm");
VelocityContext vc = new VelocityContext();
Map<String, Object> jsonResponse = new HashMap<String, Object>();
jsonResponse.put("44", "United Kingdom");
jsonResponse.put("33", "France");
jsonResponse.put("49", null);
vc.put("userList", jsonResponse);
StringWriter writer = new StringWriter();
template.merge(vc, writer);
System.out.println(writer);
}
在 .vm 文件中
#foreach ($mapEntry in $userList.entrySet())
$!{mapEntry.get("44")}
#end
在这里我试图使用上面的代码获得特定的价值,但它没有给出预期的输出
我的预期输出是
United Kingdom
【问题讨论】:
-
而实际输出是?
-
mapEntry 是...一个地图条目。是否有任何 get() 方法需要 Map.Entry 中的键?如果您只需要一个特定的值,为什么还要迭代地图条目?您将如何在 Java 代码中获得该特定值?
-
我不使用 Velocity,但是当您只需要直接来自 Map 的单个值时,为什么要遍历
entrySet?
标签: java velocity velocity-template-language