如何访问速度模板文件中的地图 [英] How to access map in velocity template file

查看:17
本文介绍了如何访问速度模板文件中的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在速度模板文件中传递一个 Map 时,当我尝试打印 map 的值时,它会被排序(基于 ASCII 值).

我的做法如下:

这是我的速度模板文件::

#set($tocList=${mapReference.mapValue})#set($tocEntry="")<div >#foreach($tocList.keySet() 中的 $tocEntry)<a href="#$tocEntry">$tocList.get($tocEntry)</a><br/>#结尾

我的 Java 代码是:

 Mapmap=new HashMap();Mapm1=new HashMap();\\我们要在模板文件中打印的值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());模板 t = velocityEngine.getTemplate("tocTemplate.vm");StringWriter writer = new StringWriter();t.merge(context , writer);System.out.println(writer);

输出为:

 

<a href="#dghjdhdf">gI Ml</a><br/><a href="#chdgfhd">gBc Jk</a><br/><a href="#sdfhfg">Df lm</a><br/>

为什么这些值会被排序?我想按原样打印地图.

解决方案

HasMap 的顺序不会随时间保持不变,来自 Javadoc:

<块引用>

这个类不保证地图的顺序;在特别是,它不保证订单将保持不变随着时间的推移.

为了保持顺序,可以考虑使用LinkedHashMap 如下建议:

<块引用>

这个链表定义了迭代顺序,通常是键插入映射的顺序

when I passed a Map<String,String> in velocity template file, and when try to print the values of map it get sorted (on the basis of ASCII values).

I am doing as follows:

this is my velocity template file::

#set($tocList=${mapReference.mapValue})
#set($tocEntry="")

<div >
 #foreach($tocEntry in $tocList.keySet())
  <a href="#$tocEntry">$tocList.get($tocEntry)</a><br/>
 #end
</div>

My Java code is :

 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);

Output is:

 <div>
   <a href="#dghjdhdf">gI Ml</a><br/>
   <a href="#chdgfhd">gBc Jk</a><br/>
   <a href="#sdfhfg">Df lm</a><br/>
 </div>

Why these values get sorted? I want to print map as it is.

解决方案

The order of the HasMap will not remain constant over time, from Javadoc:

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.

To remain the order, you can consider use LinkedHashMap as following suggested:

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map

这篇关于如何访问速度模板文件中的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆