dai*_*isy 6 java velocity velocity-template-language
我有一个像下面这样的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);
}
Run Code Online (Sandbox Code Playgroud)
在 .vm 文件中
#foreach ($mapEntry in $userList.entrySet())
$!{mapEntry.get("44")}
#end
Run Code Online (Sandbox Code Playgroud)
在这里,我试图使用上面的代码获得特定的价值,但它没有给出预期的输出
我的预期输出是
United Kingdom
Run Code Online (Sandbox Code Playgroud)
使用此代码迭代您的地图值。它和你的几乎一样,但要注意:$userList.keySet()代替$userList.entrySet()和$!{userList.get($mapKey )}代替$!{mapEntry.get("44")}
#foreach($mapKey in $userList.keySet())
$!{userList.get($mapKey )}
#end
Run Code Online (Sandbox Code Playgroud)
如果您只想访问地图的特定值,请尝试以下操作:
$!{userList.get("44")}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6007 次 |
| 最近记录: |