java – 如何在速度模板文件中访问地图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何在速度模板文件中访问地图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1472字,纯文字阅读大概需要3分钟。
内容图文
![java – 如何在速度模板文件中访问地图](/upload/InfoBanner/zyjiaocheng/748/310da647a22b4b3cacd197603b961b2c.jpg)
当我传递Map< String,String>在速度模板文件中,当尝试打印map的值时,它会被排序(基于ASCII值).
我这样做:
这是我的速度模板文件::
#set($tocList=${mapReference.mapValue})
#set($tocEntry="")
<div >
#foreach($tocEntry in $tocList.keySet())
<a href="#$tocEntry">$tocList.get($tocEntry)</a><br/>
#end
</div>
我的Java代码是:
Map<String, String> map=new HashMap<String, String>();
Map<String,HashMap> m1=new HashMap<String, HashMap>();
\\values that we want to print in template file
map.put("sdfhfg", "Df lm");
map.put("chdgfhd", "gBc Jk");
map.put("dghjdhdf", "gI Ml");
m1.put("mapValue", (HashMap) map);
VelocityEngine velocityEngine = VelocityEngineFactory.getVelocityEngine();
VelocityContext context = new VelocityContext();
context.put("img",model);
context.put("mapReference",m1);
context.put("iterator", new IteratorTool());
Template t = velocityEngine.getTemplate("tocTemplate.vm");
StringWriter writer = new StringWriter();
t.merge(context , writer);
System.out.println(writer);
输出是:
<div>
<a href="#dghjdhdf">gI Ml</a><br/>
<a href="#chdgfhd">gBc Jk</a><br/>
<a href="#sdfhfg">Df lm</a><br/>
</div>
为什么这些值会被排序?我想按原样打印地图.
解决方法:
从Javadoc开始,HasMap的顺序将不会保持不变:
This class makes no guarantees as to the order of the map; in
particular, it does not guarantee that the order will remain constant
over time.
要保留订单,您可以考虑使用LinkedHashMap,如下所示:
This linked list defines the iteration ordering, which is normally the
order in which keys were inserted into the map
内容总结
以上是互联网集市为您收集整理的java – 如何在速度模板文件中访问地图全部内容,希望文章能够帮你解决java – 如何在速度模板文件中访问地图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。